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 disas...
CPython allows access to the code object for a function object.
The __code__object contains the raw bytecode (co_code) of the function as well as other information such as constants and variable names.
def fib(n):
if n <= 2: return 1
return fib(n-1) + fib(n-2)
dir(fib.__code__)
de...
Objects that are not built-in
To print the source code of a Python object use inspect. Note that this won't work for built-in objects nor for objects defined interactively. For these you will need other methods explained later.
Here's how to print the source code of the method randint from the ran...