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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 15:16:19

问题


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:


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.



来源:https://stackoverflow.com/questions/28776723/how-to-see-the-assembly-code-of-a-python-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!