Sabine Maennel | python-articles

Here you can find my writing about Python

Understanding Generators in Python

11.7.2016

In this tutorial I show you how to make a generator in Python. A generator is a function, that will serve you a sequence: it knows what “next” means in a given context. The sequence it serves can be finite or infinite.

What to expect:

In this tutorial you will learn:

Tutorial: Generators in Python

Prezi Presentation (takes some seconds to load): scroll through at your own speed.

Revisiting the content

1. What is a generator in Python?

a function that knows how to give you a next item A generator serves next() by using yield

Code Example: A simple generator (slide 8)

>>>def my_generator():
           yield "How"
           yield "are"
           yield "you"
           yield "today"  
>>>x = my_generator() # x is a generator
>>>next(x)
'How' 
>>>next(x)
'are'
>>>next(x)
'you'
>>>next(x)
'today'
>>>next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

When a generator stops

When Calling a generator

Code Example: calling a generator (slide 12)

>>>def my_generator():
           yield "How"
           yield "are"
           yield "you"
           yield "today"  
>>>x = my_generator() # x is a generator
>>>
while True:
    try:
         next(x)
    except StopIteration:
         print('there is no next item')
         break 
'How'
'are'
'you'
'today'
there is no next item

2. The difference between a generator and an iterator

Iterators serve from a iterable Generator generate out of the thin air

3. Building a generator

Code example of an infinite generator (slide 18)

printing multiples of a number

>>>def my_multiples_of_3_generator(base):
           n = 1
           while True:
               yield base * n
               n += 1
>>>y = my_multiples_of_3_generator(3)
>>>next(y)
6
>>>next(y)
3
>>>next(y)
9

Code Example of a finite generator (slide 20)

printing dividers of a number

>>>def my_divider_generator(number):
            for n in range(1, number):
                 remainder = number % n
                 if remainder == 0:
                     yield n
>>>
while True:
    try:     
         dividers.append(next(x))      
    except StopIteration:
         print(dividers)
         break
>>>dividers =[]
>>>x = my_divider_generator(14)
[1, 2, 7]

4. Looking at iterators

What is an iterator? (slide 20)

Python collections are iterable: (slide 21)

Collections are iterable means: (silde 23)

Code Example: getting an iterator from an iterable (slide 26)

>>>my_iterable = ['How', 'are', 'you', 'today']
>>>x = iter(my_iterable) # x is a iterator
>>>next(x)
'you'
>>>next(x)
'How' 
>>>next(x)
'are'
>>>next(x)
'today'
>>>next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration