`goto`在Python中 [英] `goto` in Python

查看:243
本文介绍了`goto`在Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在Python中使用 goto 。我阅读了



但是,可能有一些教诲案例你实际上需要一个 goto



这个python配方提供 goto 命令作为函数装饰器。



The goto decorator Python recipe by Carl Cerecke


如果你生病了
现有 goto 模块的速度太慢 http://entrian.com/goto/ 。这个
配方中的 goto 大约快60倍,也更干净(滥用 sys.settrace
似乎几乎pythonic)。因为这是一个装饰器,它警告
阅读器哪些函数使用 goto 。它不实现来自
命令,虽然不难将其扩展到这样做(练习
为读者)。此外,不支持计算的gotos;他们不是
pythonic。




  • 使用 dis.dis(fn)显示函数的字节码反汇编。

  • 函数的字节码由 fn.func_code.co_code 访问。
    这是只读的:

  • 装饰函数创建与旧的
    完全相同,但是字节码更新为服从 goto 命令。

  • 这是仅2.x;新的模块不在python 3.x(读者的另一个
    练习!)


使用

  @goto 
def test1(n):
s = 0

标签.myLoop

如果n <= 0:
return s
s + = n
n - = 1

goto .myLoop

>>>> test1(10)
55






strong>更新



这里有两个与Python 3兼容的额外实现:




I must use goto in Python. I read this but my Python implementation (CPython 2.7.1 on Mac) does not have this module, so it doesn't seem to be portable. It should at least work in all Python implementations which support CPython bytecode (esp. I care about CPython and PyPy).

I could go and build up the bytecode manually (i.e. write my own Python compiler) because there is such an instruction (JUMP_ABSOLUTE and friends). But I wonder if there is an easier way. Is it possible via inspect or so to call a single bytecode instruction? I also thought about compiling via Python and then automatically patching the generated Python bytecode.


Of course, people will ask why and will not give me any helpful answer if I don't explain why I really really need this. So in short my use case: I am translating a C AST to Python AST and compiling this. I can map every logical flow (all the loops and other stuff) in some way to equivalent Python code. Everything except goto. Related projects: PyCParser (see interpreter.py), PyCPython, PyLua.

解决方案

I know what everybody is thinking:

However, there might be some didactic cases where you actually need a goto.

This python recipe provides the goto command as a function decorator.

The goto decorator (Python recipe by Carl Cerecke)

This is the recipe for you if you are sick of the slow speed of the existing goto module http://entrian.com/goto/. The goto in this recipe is about 60x faster and is also cleaner (abusing sys.settrace seems hardly pythonic). Because this is a decorator, it alerts the reader which functions use goto. It does not implement the comefrom command, although it is not difficult to extend it to do so (exercise for the reader). Also, computed gotos aren't supported; they're not pythonic.

  • Use dis.dis(fn) to show the bytecode disassembly of a function.
  • The bytecodes of a function are accessed by fn.func_code.co_code. This is readonly so:
  • The decorated function is created exactly the same as the old one, but with the bytecode updated to obey the goto commands.
  • This is 2.x only; the new module is not in python 3.x (another exercise for the reader!)

Usage

@goto
def test1(n):
    s = 0

    label .myLoop

    if n <= 0:
        return s
    s += n
    n -= 1

    goto .myLoop

>>> test1(10)
55


Update

Here're two additional implementations compatible with Python 3:

这篇关于`goto`在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆