且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查看Python文件的汇编代码?

更新时间:2022-04-11 01:56:21

dis 模块分解代码对象(使用 __ dict __ 名称空间)。

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)