python中的goto语句_python开发中goto语句的使用

核心:pip install goto-statement

网站地址:https://pypi.org/project/goto-statement/

A function decorator to use goto in Python. Tested on Python 2.6 through 3.6 and PyPy.

Installation

pip install goto-statement

Usage

from goto import with_goto

@with_goto

def range(start, stop):

i = start

result = []

label .begin

if i == stop:

goto .end

result.append(i)

i += 1

goto .begin

label .end

return result

Implementation

Note that label .begin and goto .begin is regular Python syntax to retrieve the attribute begin from the objects with the variable names label and goto. However, in the example above these variables aren't defined. So this code would usually cause a NameError. But since it's valid syntax the function can be parsed, and results in following bytecode:

2 0 LOAD_FAST 0 (start)

3 STORE_FAST 2 (i)

3 6 BUILD_LIST 0

9 STORE_FAST 3 (result)

5 12 LOAD_GLOBAL 0 (label)

15 LOAD_ATTR 1 (begin)

18 POP_TOP

6 19 LOAD_FAST 2 (i)

22 LOAD_FAST 1 (stop)

25 COMPARE_OP 2 (==)

28 POP_JUMP_IF_FALSE 41

7 31 LOAD_GLOBAL 2 (goto)

34 LOAD_ATTR 3 (end)

37 POP_TOP

38 JUMP_FORWARD 0 (to 41)

9 >> 41 LOAD_FAST 3 (result)

44 LOAD_ATTR 4 (append)

47 LOAD_FAST 2 (i)

50 CALL_FUNCTION 1

53 POP_TOP

10 54 LOAD_FAST 2 (i)

57 LOAD_CONST 1 (1)

60 INPLACE_ADD

61 STORE_FAST 2 (i)

11 64 LOAD_GLOBAL 2 (goto)

67 LOAD_ATTR 1 (begin)

70 POP_TOP

13 71 LOAD_GLOBAL 0 (label)

74 LOAD_ATTR 3 (end)

77 POP_TOP

14 78 LOAD_FAST 3 (result)

81 RETURN_VALUE

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值