Python equivalent of call/cc?
Larry Bates
larry.bates at websafe.com`
Wed Jul 9 20:23:53 EDT 2008
The Pythonista wrote:
> Yesterday, I was hacking around a bit, trying to figure out how to
> implement the semantics of call/cc in Python. Specifically, I wanted to
> translate this Scheme code to equivalent Python:
>
> ####
>
> (define theContinuation #f)
>
> (define (test)
> (let ((i 0))
> (call/cc (lambda (k) (set! theContinuation k)))
> (set! i (+ i 1))
> i))
>
> (test)
> (theContinuation)
> (theContinuation)
>
> ####
>
> Incidentally, those last three lines evaluate to 1, 2, and 3,
> respectively.
>
> The best Python translation I could come up with was something like this
> (targeted at Python 2.5):
>
> ####
> import inspect
>
> theContinuation = None
>
> def call_cc (f):
> f (inspect.currentframe().f_back)
>
> def invoke (c):
> exec c.f_code in c.f_globals, c.f_locals
>
> def test():
> i = 0
> call_cc (lambda k: globals().update({'theContinuation' : k }))
> i = i + 1
> print i
>
> test()
> invoke (theContinuation)
> invoke (theContinuation)
>
> ####
>
> Now, this code is wrong on a number of levels [I am indeed aware of
> exactly how ugly that lambda is...], but, in particular, my
> continuations / stack frames don't seem to be resuming at the right
> point. I'd expect invoke (theContinuation) to restart on the line
> immediately following call_cc, but it does not. Not surprisingly, the
> output is also wrong. (I get 1, 1, and 1 rather than 1, 2, and 3.)
>
> Can anyone help me by, perhaps pointing out some silly error I made?
> Failing that, can someone show me a working implementation of call/cc
> (preferably based on some form of stack inspection)?
>
> Thanks!
While I know absolutely no scheme what you show here is a generator in Python.
def theContinuation():
n = 0
while 1:
n += 1
yield n
invoke = theContinuation()
print
print invoke.next()
print invoke.next()
print invoke.next()
Maybe that is what you are looking for?
-Larry
More information about the Python-list
mailing list