Display the bytecode of a function
suggest changeThe Python interpreter compiles code to bytecode before executing it on the Python’s virtual machine (see also http://stackoverflow.com/documentation/python/1763/the-dis-module/5729/what-is-python-bytecode#t=201609280954269606205).
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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents