Turn Messy Text into Structured Insights With AI. Join Greg Live:Β Dec 4th, 2024
Leverage
Glossary

Generator

Python generator usage and characteristics

In Python, Generator functions are special functions that return a lazy iterator (or an iterator that is only used when you call it). Lazy iterators are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their items in memory. This makes them memory efficient when working with large data.

# Generator function for an infinite sequence
def my_generator():
    x = 0
    while True:
        yield x
        x += 1

next(g)
>>> 0
next(g)
>>> 1

The difference is that while a return statement terminates a function entirely, yield statement pauses the function saving all its states and later continues from there on successive calls.

Python Generator

The performance improvement from the use of python generators is the result of the on-demand generation of values. This means we don’t need to wait for values to be generated to use them. We can create and use them one by one.

Note: Generator will provide performance benefits only if we do not intend to use that set of generated values more than once.

Generators are an advanced Python topic. 99% of the time regular functions (with return statements) will work for you code. If you are running into performance problems, consider using generators with your loops.

Let’s take a look at a python generator code sample.

Link to code

On this page