Info on continuations?

Michael ms at cerenity.org
Tue Aug 8 14:27:11 EDT 2006


vasudevram wrote:

> 
> Hi,
> 
> I am Googling and will do more, found some stuff, but interested to get
> viewpoints of list members on:
> 
> Continuations in Python.
>
> Saw a few URLs which had some info, some of which I understood. But
> like I said, personal viewpoints are good to have.

Python doesn't really support continuations. Generators (and co-routines in
general) have similar properties to continuations, however they're not
continuations. Closures are also sometimes considered cousins to
continuations, and python's support for those is pretty good IMO. (again
however, closures are not continuations).

Since it looks like you're also looking for what continuations are, I think
the following is the simplest way of explaining them. If you've ever
programmed in BASIC, *in a way* if you think of methods/functions as a
named GOSUB, then continuation (in a way) are a way of giving a name to a
goto (it's more subtle than that though since a continuation os often
defined to "remember" state in a similar way to a closure). Like a function
you can pass them round as objects. Like a generator/closure they remember
the state they were in. 

*Personally* , I think python NOT supporting full continuations is a GOOD
thing, since full continuations, whilst powerful, are also a great source
of confusion for people. (That said, I have a usecase I'd find them useful
for - I think they'd be useful for plugin architectures - but IMO that
doesn't outweigh the risk of code obfuscation :-)

One particular usecase that people seem to like continuations for,
specifically how they're used in seaside, is actually a subset of
functionality that python *can* support (to a large extent).

Essentially the idea is to be able to take a web application and make it
look linear. CherryFlow allows you for example to write this:

@expose
@flow
def example_flow():
    yield view.first_page()
    if request.args["choice"] == "a":
        yield view.choice_a_page()
    else:
        yield view.choice_b_page()
    yield view.last_page()

(example from: http://tinyurl.com/qzpqu )

This causes the user's browser to show a page where they have a choice of
"a" or "b". Depending on what they choose, they then either are presented
with choice_a_page or choice_b_page. Finally, no matter which option they
chose, you are presented with the last page.

Something similar can be done using Kamaelia, though at present only the
mechanism exists there, without any sugar (http://tinyurl.com/n3bh7 -
specifically websiteSessionExampleComponent). The reason I mention it is to
say that it's relatively simple to do using python :-)

I have to stress though, this usage of continuations in Seaside, as
emulate-able in python is a subset of the full power of continuations,
which isn't available in python at present. (Though it's possible hacking
greenlets could result in something). (In fact the way seaside uses them as
far as I can tell is more to implement co-routine like behaviour than
anything else (!))

Anyway, hope that's interesting/useful - looking at your other comments,
you're just looking for information and usecases at the moment :-)

Regards,


Michael.




More information about the Python-list mailing list