定义函数
打开python解释器,输入如下代码,定义了一个简单的函数foo,
1 2 3 4 5 6 7 8 9 10
| zzq@ubuntu:~$ python3 Python 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def foo(): ... a=2 ... b=3 ... c=a+b ... return c ...
|
字节码反编译
使用内置的dis模块可得到函数foo字节码的汇编代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| >>> import dis >>> dis.dis(foo) 2 0 LOAD_CONST 1 (2) 2 STORE_FAST 0 (a)
3 4 LOAD_CONST 2 (3) 6 STORE_FAST 1 (b)
4 8 LOAD_FAST 0 (a) 10 LOAD_FAST 1 (b) 12 BINARY_ADD 14 STORE_FAST 2 (c)
5 16 LOAD_FAST 2 (c) 18 RETURN_VALUE
|
局部变量表
首先分析出函数foo的局部变量表如下,
执行流程分析
字节码执行流程如下,
Line 2:
1 2 3 4
| LOAD_CONST 1
STORE_FAST 0
|
Line 3:
1 2 3 4
| LOAD_CONST 2
STORE_FAST 1
|
Line 4:
1 2 3 4 5 6 7 8
| LOAD_FAST 0
LOAD_FAST 1
BINARY_ADD
STORE_FAST 2
|
Line 5:
1 2 3 4
| LOAD_FAST 2
RETURN_VALUE
|