Both coroutines and generators use Python’s yield
keyword, but they serve different purposes. Here’s a breakdown:
- A generator is a function that produces a sequence of values lazily (on-demand) using
yield
. - It pauses execution at each
yield
and resumes when the next value is requested (e.g., in afor
loop).