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

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.
dis.dis(fib)

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



Got any Python Language Question?