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.
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.
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With