class t(): pass class t2(): pass def test1(): a=10 b=t() d=t2() e=t2() def f1(c): print a print b print c f1()
Disassembly of test1: 19 0 LOAD_CONST 1 (10) 3 STORE_DEREF 0 (a)#存入到freevars 20 6 LOAD_GLOBAL 0 (t)#从全局变量中装入 9 CALL_FUNCTION 0 12 STORE_DEREF 1 (b) 21 15 LOAD_GLOBAL 1 (t2) 18 CALL_FUNCTION 0 21 STORE_FAST 0 (d) 22 24 LOAD_GLOBAL 1 (t2) 27 CALL_FUNCTION 0 30 STORE_FAST 1 (e) 23 33 LOAD_CLOSURE 0 (a) 36 LOAD_CLOSURE 1 (b) 39 BUILD_TUPLE 2 #生成闭包 42 LOAD_CONST 2 (<code object f1 at 01387608, file "<module1>", line 23>) 45 MAKE_CLOSURE 0#创建一个闭包函数 48 STORE_FAST 2 (f1) 27 51 LOAD_FAST 2 (f1) 54 CALL_FUNCTION 0 57 POP_TOP 58 LOAD_CONST 0 (None) 61 RETURN_VALUE