Generator Functions in Python - Deepstash
How generator functions work

How generator functions work

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

IDEAS CURATED BY

ocpodariu

Alt account of @ocp. I use it to stash ideas about software engineering

CURATOR'S NOTE

📖 Fluent Python

Other curated ideas on this topic:

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.

Email

I agree to receive email updates