Python相当于Ruby的延续

bha*_*dra 7 ruby python continuations

Ruby中以下代码的Python等价物是什么?

def loop
  cont=nil
  for i in 1..4
    puts i
    callcc {|continuation| cont=continuation} if i==2
  end
  return cont
end

> c=loop
1
2
3
4
> c.call
3
4
Run Code Online (Sandbox Code Playgroud)

参考:轻量级开发成功的秘诀,第9部分:基于Continuations的框架

Gre*_*ill 5

您引用的文章在参考资料部分中包含指向Continuations Made Simple和I​​llustrated的链接,该部分讨论了Python语言中的延续.