Chapter 5. Iterators and Generators

When many people with experience in another language start learning Python, they are taken aback by the difference in for loop notation. That is to say, instead of writing:

# Other languages
for (i=0; i<N; i++) {
    do_work(i);
}

they are instead introduced to a new function called range or xrange:

# Python
for i in range(N):
    do_work(i)

These two functions provide insight into the paradigm of programming using generators. In order to fully understand generators, let us first make simple implementations of the range and xrange functions:

def range(start, stop, step=1):
    numbers = []
    while start < stop:
        numbers.append(start)
        start += step
    return numbers

def xrange(start, stop, step=1):
    while start < stop:
        yield start # 1
        start += step

for i in range(1,10000):
    pass

for i in xrange(1,10000):
    pass
1

This function will yield many values instead of returning one value. This turns this regular-looking function into a generator that can be repeatedly polled for the next available value.

The first thing to note is that the ...

Get High Performance Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.