Iterators & generators (RE: Real Problems with Python)

Roberto Lupi Roberto.Lupi at ascu.unian.it
Sat Feb 19 18:06:24 EST 2000


In article <887osh$1rv$1 at nntp6.atl.mindspring.net>, aahz at netcom.com 
says...

[binary tree traversal implemented with Icon-like suspend]

> >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.

No it can and should have different semantics (IIRC in Icon the semantic 
of suspend is different from what you have described).

What Tim calls "the next call" is better expressed as "the next 
activation": every call to a function that suspends can return multiple 
values as results, the first result is computed (up to the suspend 
statement) and this result is returned, when that specifica call to the 
function is reactivated the code restart as if there was no suspend 
statement until suspend is executed again or the function exists.
A new call to the "resumable function" do _not_ share the stack saved 
with a different call to the same function.

-- 
Roberto Lupi
IT Consultant
Software Developer



More information about the Python-list mailing list