Python Programming - Generators - Discussion
Discussion Forum : Generators - General Questions (Q.No. 81)
81.
What happens if a generator function contains a
return
statement followed by a yield
statement?
Answer: Option
Explanation:
If a generator function contains both
return
and yield
statements, it returns the specified value and stops the generator.
def my_generator():
return 42
yield # This statement is never reached
# Example usage
gen = my_generator()
result = next(gen, None)
print(result) # Output: 42
Discussion:
Be the first person to comment on this question !
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers