How to see the assembly code of a Python file?

disassembly, python

Solution

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.

Problem

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?

Original source