Python Language Accessing Python source code and bytecode Display the bytecode of a function


The Python interpreter compiles code to bytecode before executing it on the Python's virtual machine (see also What is python bytecode?.

Here's how to view the bytecode of a Python function

import dis

def fib(n):
    if n <= 2: return 1
    return fib(n-1) + fib(n-2)

# Display the disassembled bytecode of the function.

The function dis.dis in the dis module will return a decompiled bytecode of the function passed to it.