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

CURATED BY

ocpodariu

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

CURATOR'S NOTE

📖 Fluent Python

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.

Email

I agree to receive email updates