Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuations in python

Tags:

python

is there any equivalent construct for continuations in python. I have been reading about continuations and am just being curious and want to know as there is nothing about that in the docs.

like image 584
cobie Avatar asked Apr 27 '12 22:04

cobie


People also ask

How do continuations work?

Continuations allow you to literally "jump" to different places in your code. They are a low-level primitive that gives you control over execution flow, allowing you implement everything from resumable exceptions to coroutines.

What are continuations in scheme?

In Scheme, continuations are first-class objects, just like functions. You can ask Scheme for the current continuation, and it will make you a function of one argument representing the future of the computation.

How do you call a function in Python?

How to call a function in python? Once we have defined a function, we can call it from another function, program, or even the Python prompt. To call a function we simply type the function name with appropriate parameters.


1 Answers

my question is more in the line of if continuations are supported in python then how can they be expressed?

They are expressed the same way in Python as in any other language without call/cc: by passing a function as the continuation argument to another function. Consider the very silly example of a continuation that is applied to the sum of two numbers. In ML, you might write

fun plus(x, y, k) = k(x + y)

plus(2, 4, print o Int.toString)

which prints 6.

but in Python you might write

def plus(x, y, k):
    return k(x + y)

plus(2, 4, lambda n: print '%d' % n)

which also prints 6.

like image 165
Adam Mihalcin Avatar answered Sep 21 '22 02:09

Adam Mihalcin