A generator function is any function that has the yield keyword in its body. When called, generator functions return a generator object that wraps the body of the function.
Generator objects can be consumed like any other iterator. When we call next() on them, execution advances to the next yield statement in the function body, and the yielded value is returned. Finally, when the function body returns, the generator object returns StopIteration.
See example in image: gen_AB() returns a generator that can be iterated over. next() is called on it 3 times in total.
2
32 reads
MORE IDEAS ON THIS
CURATED FROM
IDEAS CURATED BY
Alt account of @ocp. I use it to stash ideas about software engineering
๐ Fluent Python
โ
Read & Learn
20x Faster
without
deepstash
with
deepstash
with
deepstash
Personalized microlearning
โ
100+ Learning Journeys
โ
Access to 200,000+ ideas
โ
Access to the mobile app
โ
Unlimited idea saving
โ
โ
Unlimited history
โ
โ
Unlimited listening to ideas
โ
โ
Downloading & offline access
โ
โ
Supercharge your mind with one idea per day
Enter your email and spend 1 minute every day to learn something new.
I agree to receive email updates