Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python's Generator and Yield Explained

2 min read

Table of Contents

Generators are iterators, a kind of iterable you can only iterate over once.

So what are iterators anyway?

An iterator is an object that can be iterated (looped) upon. It is used to abstract a container of data to make it behave like an iterable object. Some common iterable objects in Python are - lists, strings, dictionary.

Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions.

The yieldkeyword behaves like return in the sense that values that are yielded get "returned" by the generator. Unlike return, the next time the generator gets asked for a value, the generator's function, resumes where it left off after the last yield statement and continues to run until it hits another yield statement.

In simpler words, a generator is simply a function that returns a generator object on which you can call next() such that for every call it returns some value until it raises a StopIteration exception, signaling that all values have been generated.

Let's start with creating some generators

def my_generator():
    yield "First iterator object"
    yield "Second iterator object"
    yield "Third iterator object"

As per the definition, the generator function creates a generator object you can verify this.

my_generator()

Output:

<generator object my_generator at 0x7f0870635258>

Store this object in a variable and call the next() method on it. Every call on next() will yield a single value until all the values have been yield.

generator_object = my_generator()
next(generator_object)

Output:

First iterator object

Again from the definition, every call to next will return a value until it raises a StopIteration exception, signaling that all values have been generated so for this example we can call the next method 3 times since there are only 3 yield statements to run.

2nd Iteration

next(generator_object)

Output:

Second iterator object

3rd Iteration

next(generator_object)

Output:

Third iterator object

If you call next(generator_object) for the fourth time, you will receive StopIteration error from the Python interpreter.

Let's look at another example.

import random

def number_generator():
    while True:
        number = random.randint(0,100)
        yield number

Here the generator function will keep returning a random number since there is no exit condition from the loop.

num = number_generator()next(num)

Output:

65

Great, but when are generators useful?

The key advantage to generators is that the "state" of the function is preserved, unlike with regular functions where each time the stack frame is discarded, you lose all that "state". Also, generators do not store all the values in memory instead they generate the values on the fly thus making the ram more memory efficient.

Wrapping it up

Generator functions are ordinary functions defined using yield instead of return. When called, a generator function returns a generator object, which is a kind of iterator - it has a next() method. When you call next(), the next value yielded by the generator function is returned.


PYTHON

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read