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
Alt account of @ocp. I use it to stash ideas about software engineering
📖 Fluent Python
“
More like this
1 idea
Thinking Recursively in Python – Real Python
realpython.com
3 ideas
4 ideas
An introduction to Python Type Hints
peps.python.org
Read & Learn
20x Faster
without
deepstash
with
deepstash
with
deepstash
Access to 200,000+ ideas
—
Access to the mobile app
—
Unlimited idea saving & library
—
—
Unlimited history
—
—
Unlimited listening to ideas
—
—
Downloading & offline access
—
—
Personalized recommendations
—
—
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