[Tutor] 2.7.3 generator objects

eryksun eryksun at gmail.com
Sun Sep 2 08:57:09 CEST 2012


On Sun, Sep 2, 2012 at 1:44 AM, Ray Jones <crawlzone at gmail.com> wrote:
>
> I was playing with os.walk today. I can use os.walk in a for loop (does
> that make it an iterator or just an irritable? ^_^),

The output from os.walk is a generator, which is an iterator. os.walk
actually calls itself recursively, creating a chain of generators.
Take a look:

    print inspect.getsource(os.walk)

> os.walk to 'test' (test = os.walk(<path>)), that variable becomes a
> generator object that does not work in a for loop.

You'll have to provide more information. That should work fine if you
haven't already exhausted the generator.

> it's supposed to work in a generator function using 'yield', but I'm at
> a loss at how that all works.
>
> I suppose I should just stick with using the os.walk in the for loop,
> but I'd like to make sense of the whole thing. Please someone explain
> this to me?

A generator function is a function that uses the keyword "yield"
instead of "return" (an empty return statement is allowed). When you
call a generator function, the return value is a generator object.
Think of the generator function as a factory for generator objects. A
return in the generator function (implicit or with a "return"
statement) corresponds to the generator object raising StopIteration.

A generator object is an iterator. Specifically, it has the methods
__iter__ and "next" (in 3.x it's __next__), and "iter(genobject) is
genobject".

To be an iterable in general, it suffices to have either an __iter__
method or a __getitem__ method. Here are the glossary definitions:

http://docs.python.org/glossary.html#term-iterable
http://docs.python.org/glossary.html#term-iterator

Also, here is the PEP for simple generators:

http://www.python.org/dev/peps/pep-0255/


More information about the Tutor mailing list