Help me understand this iterator

Fredrik Lundh fredrik at pythonware.com
Tue Oct 31 06:58:28 EST 2006


LaundroMat wrote:

> Now, if I look at this script step by step, I don't understand:
> - what is being iterated over (what is being called by "file in
> DirectoryWalker()"?);

as explained in the text above the script, this class emulates a 
sequence.  it does this by implementing the __getindex__ method:

     http://effbot.org/pyref/__getitem__

> - where it gets the "index" value from;

from the call to __getitem__ done by the for-in loop.

> - where the "while 1:"-loop is quitted.

the loop stops when the stack is empty, and pop raises an IndexError 
exception.

note that this is an old example; code written for newer versions of 
Python would probably use a recursing generator instead (see the source 
code for os.walk in the standard library for an example).

</F>




More information about the Python-list mailing list