Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 84)
84.
What is the purpose of the generator.send(value) method when used with generators?
It sends a value to the generator and resumes its execution
It stops the generator and raises a StopIteration exception
It raises a GeneratorExit exception
It has no effect on the running generator
Answer: Option
Explanation:
generator.send(value) sends a value into the generator, which is received as the result of the current yield expression, and resumes the generator's execution.
def my_generator():
    result = yield
    print(f"Received value: {result}")

gen = my_generator()
next(gen)            # Start the generator
gen.send(42)         # Output: Received value: 42
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.