Little Intro to Python Generators

What are python generators and how do they work?

Generators are a python feature that became standard in Python 2.3. Along with the generator came the yield keyword. One simple way I think about Python generators is that they are a way to return multiple items from a function one by one, instead of returning only once like normal functions.

The presence of the yield keyword in a function makes it a generator function so that when the function is called it returns a generator object.

A function that contains the yield keyword is not really a generator itself, but rather a generator factory. Meaning that a function containing the yield keyword when called will create a generator object. When a function needs to generate a value, instead of calling return, which would terminate the function, we can say yield instead. The keyword yield does these things:

  • passes a value back to whoever called it
  • bookmarks its place until it is called again, then it resumes where it left off

Things you can do with a generator object:

  • Use the generator object in the place of a list in a for in loop.
Because a generator object is an iterable, you can use it in place of a list in a for statement or other loops that support the iterator protocol.
>>> def i_return_a_generator():
yield 'I contain the keyword yield'
yield 'So I am a generator function'
yield 'And I return a generator object'
yield 'that I can iterate over'
>>> i_return_a_generator()
<generator object i_return_a_generator at 0x1071280a0>
>>> for x in i_return_a_generator():
print x
I contain the keyword yield
So I am a generator function
And I return a generator object
that I can iterate over
  • From a single generator function you can create many generator objects that all behave independently
The next method retrieves the next item from the iterator
def generatorFunction():
yield "Im a generator function (i.e factory)"
yield "Cause I contain the yield keyword"
generatorObject1 = generatorFunction()
generatorObject1.next() = "Im a generator factory"
generatorObject1.next() = "Cause I contain the yield keyword"
generatorObject2 = generatorFunction()
generatorObject2.next() = "Im a generator factory"
  • Generators save space in memory by computing values when they are needed, instead of storing them in memory for use later.

To sum this up, generator functions can be identified by containing the keyword yield. And generator functions are cool because they provide a way create iterators and “return” many times in a single function.

Resources:

PEP 255 Simple Generator

Python Docs Yield Expression

Show your support

Clapping shows how much you appreciated Jessica G’s story.