Benefits of continuation? (EXAMPLES)

Courageous jkraska1 at san.rr.com
Fri Nov 3 21:56:02 CET 2000


>Could I have a look at some snippets of examples to
>get the feeling of how it works in reality? It would be
>really nice 'cause reading those rather abstract documents
>gives me only vague images of the castle in the air.

I can try to give you some ideas for how they work, but you
have to understand that some of the hardest code in the world
to understand is continuation code. It creates a spaghetti
stack, with rather mysterious flow-control semantics.  Instead
of showing you some actual code of mine, I'll contrive some
examples:

---------------------------------------------------------------------------------------
test.py:

import continuation

def main():

    print "before"
    f()
    print "after"

def f():

    c = continuation.caller(1)
    c.jump()

main()

py test.py
before
after
-----------------------------------------------------------------------------------------

In this example, nothing in particular is achieved. The continuation
"c" is generated on the cal "continuation.caller(1)", which stands
for the continuation point of whoever called the f() function. When I
jump to the continuation-point (defined by c), I'm going to usual
return point that I'd have used anyway had I simply returned from
f().

Here is another example:
-----------------------------------------------------------------------------------------
class Holder:
    pass

def main():

    h = Holder()
    print "before f()"
    f(h)
    print "before g()"
    g(h)

def f(h):

    h.c = continuation.caller(1)

def g(h):

    h.c.jump()

main()

py test.py
before f()
before g()
before g()
before g()
....
-----------------------------------------------------------------------------------------
In this one, I have effectively used the continuation object to
construct a label/goto tag. Really pretty disgusting thing to do
to our friend Python, but so it goes. What's actually happening
is I'm making f() return as many times as I wish it to. 

Below is an example of calling jump() with a value. When the
continuation is *set up differently*, you can examine a return
value when it is invoked. The example somewhat speaks for
itself:
-----------------------------------------------------------------------------------------
import continuation

class Holder:
    pass

def main():

    h = Holder()
    print "before f()"
    f(h)
    print "before g()"
    g(h)
    print "goodbye, friendly example"

def f(h):

    h.callcount = 0
    h.c = continuation.current()
    rv = h.c.update(None)
    if rv != None: ## first time through update() returns the value I gave it
        print rv

def g(h):

    if h.callcount < 10:
        h.callcount = h.callcount + 1
        h.c.jump(h.callcount*2)

main()

py test.py
before f()
before g()
2
before g()
4
before g()
6
before g()
8
before g()
10
before g()
12
before g()
14
before g()
16
before g()
18
before g()
20
before g()
goodbye, friendly example
-----------------------------------------------------------------------------------------

Note what is happening. I keep a count of the number of times
I've invoked the continuation, and then return a value. The
callcount*2 expression was aribitrary, and really could have been
any value at all. Using this trick, it's possible to cause the same
function to return many different times, with many different values.

Once you begin thinking about flow control and continuations,
you'll see that this opens up many different possibilities for things
you can do with them.

Note, however, how hairy continuation code could become to
understand. Take care, and confine their use to the very specific
cases in which you need them.

Hope this helps.

I think if you fiddle with the examples, you'll get the idea right
away. Just think of a continuation as a sort of dynamic goto
point, which can jump function invocation boundaries, and is
polite enough to restore the local-variable state to the state
the local variables were in when the continuation was set.


C//




More information about the Python-list mailing list