.Tang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实例:https://www.cnblogs.com/tangpg/p/7992979.html

 

在系统内部,解释器使用一种被称为 ‘块栈’的结构处理异常逻辑。它和执行栈一起被栈帧管理。块栈在运行期间,相关指令会提前将跳转位置信息存储到块栈,需要的时候从中获取。

调试: __debug__

test.py

def test():
    if __debug__:
        print('debug')
        global x
    else:
        print('else')
    x = 10101
test()
print(x)
import dis
dis.dis(test)
>>> python -O test.py   # -O是启动解释器优化模式,致使debug=0,但代码分析器依然认为x 为全局变量
else
10101
227           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('else')
              4 CALL_FUNCTION            1
              6 POP_TOP

228           8 LOAD_CONST               2 (10101)
             10 STORE_GLOBAL             1 (x)      # 编译器忽略了
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE

 

posted on 2018-09-03 17:19  .Tang  阅读(184)  评论(0编辑  收藏  举报