Scheme style and Python style [was: Re: Typed Python?]

Christopher T King squirrel at WPI.EDU
Wed Jul 7 22:00:10 CEST 2004

On Wed, 7 Jul 2004, [iso-8859-1] François Pinard wrote:

> I recently worked on my little `pynits' tool (hoping to release it a
> third time, as soon as I'm satisfied enough with the changes), and have
> a need for continuations there.  The circumstances for that need may be
> slightly unusual, but I would surely not qualify them as abnormal :-).
> Suddenly backtracking out of a complex involvement for restarting in a
> new direction, for the above project at least, requires a lot of state
> resumption, and doing this cleanly in Python implies a rather strict and
> burdening discipline about where and how the state information is kept.
> While tractable, this is not as comfortable as I would have liked it...

It may be possible to do what you want to do using only generators. 
Assuming that you have some N-deep nested structure, you could do 
something like this:

def dootherstuff():
    <do some stuff>
    if <exceptional condition>:
        yield <some helpful value>
    <do more stuff>

def dostuff():
    <do some stuff>
    for value in dootherstuff():  # call dootherstuff(), propogating yields
        yield value
    <do more stuff>
    if <exceptional condition>:
        yield <some helpful value>
    <finish doing stuff>

for value in dostuff():
    <handle exceptional condition described by value>

Alternatively, you could replace the for loops with something like:

except StopIteration:
    <store continuation away for future use>
    <function completed, carry on>

Dunno if this helps or not....

More information about the Python-list mailing list