Iterators & generators (RE: Real Problems with Python)

Aahz Maruch aahz at
Sun Feb 13 21:22:09 EST 2000

In article <slrn8aeoh8.o7u.kc5tja at>,
Samuel A. Falvo II <kc5tja at> wrote:
>In article <000001bf768e$48e40580$45a0143f at tim>, Tim Peters wrote:
>>class BinTree:
>>   # with members .left and .right of type BinTree (& None means none),
>>   # and .data of an arbitrary type
>>   ...
>>   def traverse_post(self):
>>       for child in self.left, self.right:
>>           if child is not None:
>>               suspend child.traverse_post()
>>       suspend
>>b = BinTree()
>>for leaf in b.traverse_post():
>>    process(leaf)
>I'm sorry, but I can't follow this code at all.  What are the precise
>semantics of suspend here?  How does it return?

suspend *is* a return.  It also -- at the same time -- stores the
program counter for b.traverse_post(), so that the next call to
b.traverse_post() starts at the line of execution immediately following
the suspend.  This means that, should someone be foolish enough to call
b.traverse_post() inside the loop "for leaf...", the loop will probably
return incorrect results, just like modifying a list while you iterate
over it.

Using suspend means that people no longer have to hack __getitem__() to
"do the right thing" for a class.  Doing this in a threaded environment
is dangerous, but no more so than dealing with any other class.
                      --- Aahz (Copyright 2000 by aahz at

Androgynous poly kinky vanilla queer het    <*>
Hugs and backrubs -- I break Rule 6

Have you coined a word today?

More information about the Python-list mailing list