Python equivalent of call/cc?

Aahz aahz at
Thu Jul 10 05:40:40 CEST 2008

In article <be112$4874d38a$18817 at>,
The Pythonista  <none at this.time> 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)

Python relies on mutables to do this:

class holder: pass

def call():
    x = holder()
    x.counter = 0
    def cc():
        x.counter += 1
        return x.counter
    return cc

foo = call()

print foo()
print foo()
print foo()
Aahz (aahz at           <*>

"as long as we like the same operating system, things are cool." --piranha

More information about the Python-list mailing list