ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def inner():
a = yield "x"
b = yield "y"
return (a, b)
def outer():
r = yield from inner()
yield r
o = outer()
print(next(o), o.send(1), o.send(2))- A. x y (1, 2)
- B. x y (None, 2)
- C. x 1 2
- D. x y None
Correct answer: A. x y (1, 2)
yield from forwards send() to the inner generator: a = 1, b = 2. inner's return value (1, 2) becomes the value of the yield from expression, which outer then yields.
More Python Programming questions
- What is printed? python def g(): try: yield 1 yield 2 except KeyError: yield "caught" yield 3 it = g() print(next(it), it.throw(KeyError)…
- What is printed? python def g(): yield 1 raise StopIteration try: print(list(g())) except RuntimeError as e: print("RuntimeError")
- What is printed? python def g(): try: yield 1 finally: print("F", end=" ") it = g() next(it) it.close() print(next(it, "done"))
- What is printed? python import itertools a, b = itertools.tee(iter([1, 2, 3])) next(a); next(a) print(list(b), list(a))
- What is printed? python def nums(): for n in range(5): print(n, end="") yield n r = any(n > 1 for n in nums()) print("", r)
- What is printed? python gen = (i for i in range(3)) print(1 in gen, 1 in gen, list(gen))