Week 3 Extras

A Note About Generators

Django Generator Examples

We saw examples of generators being used in the Django project:

  1. Looping over GDAL/OGR features
  2. Database query functions

Questions:

  • Did you notice that almost all __iter__ function end with a yield?
  • What’s the benefit of having yield here?

Generator Review

Last time we saw that the print statement after the yield statement executes at an unexpected time

def count_to_10():
    print "executing..."
    for i in range(10):
        j = yield i
        print "continue..."
In [2]: citer = count_to_10()

In [3]: citer.next()
executing...
Out[3]: 0

In [4]: citer.next()
continue...
Out[4]: 1

We didn’t look at the stack frames in pdb for this example.

Can we expect anything different happening on the stack due to the yield statement ?

Interested in how generators work?

Look at the teaser in week-03-debugging/generators/generator_as_functions.py