Python Language Accessing Python source code and bytecode Exploring the code object 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

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__)

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


Got any Python Language Question?