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 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 then 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
Python Generator¶
Generators are like functions, but especially useful when dealing with large data. Generators will turn your function into an iterator so you can loop through it.
To create a generator, you must use yield instead of return. Generators will remember states. This means the function will remember where you left off.
Generators are an advanced Python topic, only use these if you know what you are doing! If not, 99% of the time normal functions will be fine.
# Generator function
def my_generator():
x = 1
print('Printed: First')
yield x
x = x + 1
print('Printed: Second')
yield x
x = x + 1
print('Printed: Last')
yield x
g = my_generator()
next(g)
next(g)
next(g)
If you try going too far with a generator, then you'll get an error
next(g)
Let's create another with an infinate loop
# Generator function
def my_generator():
x = 0
while True:
yield x
x += 1
g = my_generator()
next(g)
next(g)
next(g)
next(g)
next(g)
Look how we have access to an infinite set of numbers, but they aren't stored in memory anywhere. They are created and called one by one because of the generator. Cool!
Check out more Python Vocabulary on our Glossary Page