Python 中如何查看进行反汇编
dis模块
Python 反汇编是通过 dis 这个模块来查看的,一般有两种方式可以用来查看
方式一: 在命令行中使用 dis 查看
>>> def test(): ... class X: ... data = 100 ... def get(self): return self.data ... >>> >>> >>> import dis >>> dis.dis(test). # 通过dis 模块查看汇编代码 2 0 LOAD_BUILD_CLASS 2 LOAD_CONST 1 (<code object X at 0x10b454ae0, file "<stdin>", line 2>) 4 LOAD_CONST 2 ('X') 6 MAKE_FUNCTION 0 8 LOAD_CONST 2 ('X') 10 CALL_FUNCTION 2 12 STORE_FAST 0 (X) 14 LOAD_CONST 0 (None) 16 RETURN_VALUE Disassembly of <code object X at 0x10b454ae0, file "<stdin>", line 2>: 2 0 LOAD_NAME 0 (__name__) 2 STORE_NAME 1 (__module__) 4 LOAD_CONST 0 ('test.<locals>.X') 6 STORE_NAME 2 (__qualname__) 3 8 LOAD_CONST 1 (100) 10 STORE_NAME 3 (data) 4 12 LOAD_CONST 2 (<code object get at 0x10b454b70, file "<stdin>", line 4>) 14 LOAD_CONST 3 ('test.<locals>.X.get') 16 MAKE_FUNCTION 0 18 STORE_NAME 4 (get) 20 LOAD_CONST 4 (None) 22 RETURN_VALUE Disassembly of <code object get at 0x10b454b70, file "<stdin>", line 4>: 4 0 LOAD_FAST 0 (self) 2 LOAD_ATTR 0 (data) 4 RETURN_VALUE >>>
方式二,通过运行 python 文件使用 -m 参数来查看