cobie
cobie

Reputation: 7281

Continuations in Python

Is there an 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 documentation.

My question is more in the line of: If continuations are supported in Python, then how can they be expressed?

Upvotes: 10

Views: 7055

Answers (1)

Adam Mihalcin
Adam Mihalcin

Reputation: 14468

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):
    k(x + y)

plus(2, 4, print)

which also prints 6.

Upvotes: 3

Related Questions