How to see the assembly code of a Python file? [cl

2020-07-27 03:46发布

Out of curiosity I would like to see the assembly instructions that correspond to the code of a .py file. Are there any trustworthy solutions you can propose?

1条回答
够拽才男人
2楼-- · 2020-07-27 04:17

The dis module disassembles code objects (extracting those from functions, classes and objects with a __dict__ namespace).

This means you can use it to disassemble whole modules:

import dis

dis.dis(dis)

although this isn't nearly as interesting as you may think as most modules contain several functions and classes, leading to a lot of output.

I usually focus on smaller functions with specific aspects I am interested in; like what bytecode is generated for a chained comparison:

def f(x):
    return 1 < x ** 2 < 100

dis.dis(f)

for example.

查看更多
登录 后发表回答