How generator functions work - Deepstash

Bite‑sized knowledge

to upgrade

your career

Ideas from books, articles & podcasts.

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.

STASHED IN:

2

published ideas

MORE IDEAS FROM THE SAME ARTICLE

Discover and save more ideas by creating a

FREE

Deepstash account.

Develop a

reading habit

, save

time

and create an amazing

knowledge library

.

GET THE APP:

MORE LIKE THIS

Never ruin a good day by thinking about past.

1

STASHED IN:

4

published 7 ideas

📖 Reading

0 Comments

Power of python recursion

Recursive function for calculating n! implemented in Python:

Behind the scenes, each recursive call adds a stack frame (containing its execution context) to the call stack until we reach the base case. Then, the stack begins to unwind as each call returns its results:

A d...

2

STASHED IN:

5

What Is Docker?

Docker is an open source containerization toolkit which enables developers to package applications into containers.

As the name suggests, Docker is like a container ship which provides means to sort, link, manage and ship containers from a d...

STASHED IN:

6

published 6 ideas

Docker is a very powerful tool for development and code deployment. This is what you’re missing to take it to the next level!

📖 Research

0 Comments