Code object 2026-11-05
In CPython, a code object represents compiled executable Python code. It stores bytecode and metadata such as constants, referenced names and source positions. A function object additionally supplies the global namespace, default argument values and, when needed, a closure connecting free variables to captured values. A code object by itself does not contain that execution context.[ref]
For example, calling a function factory twice can create two functions with the same code object but different captured values. Their behavior differs because their closures differ, even though their instructions are shared. Inspecting f.__code__ therefore describes executable structure rather than a complete snapshot of f's environment. Python documents code objects as immutable; version-specific bytecode details should not be treated as a permanent cross-version file format.
CPython Created 2026-10-14 Updated 2026-11-05
CPython is the principal implementation of Python, developed in the Python project's source repository and distributed through python.org. It parses source code and compiles it into code objects containing bytecode, which its execution machinery evaluates. The dis module makes the bytecode instructions visible, connecting source-level functions with their compiled representation.[ref]
For example, a function that returns len(items) needs to obtain the callable and argument, perform the call and return its result. A bytecode listing reveals those operations, together with version-specific supporting instructions. CPython can specialize instructions as a program runs. Exact instruction names and layouts are implementation details that can change between releases; the meaning of the Python source expression is specified at the language level.
Function object 2026-11-05
A function object is a callable runtime object representing a function together with the context needed to execute it. In CPython, a Python-defined function object refers to a code object, its defining global namespace, default argument values and any captured bindings. The code object alone does not contain all this context.[ref]
For example, a factory make_adder(k) can return a nested function computing x+k. Calling the factory with two and five can produce function objects sharing one code object but retaining different values of k through their programming closures. Their results on input ten are twelve and fifteen. This explains why identifying compiled instructions is insufficient to identify a callable's behavior.
Interpreter (computing) Created 2026-10-13 Updated 2026-10-14
An interpreter executes a program's operations according to its programming language semantics. It can recursively evaluate an abstract syntax tree, or follow an instruction sequence using an execution state such as a stack and an instruction pointer. It need not repeatedly read source code characters, and it need not use an abstract syntax tree at all.
Compilation and interpretation can coexist. In CPython, Python source code is compiled into bytecode for execution by an interpreter; its bytecode is not promised to remain stable between releases.[ref] The Python 3.14 compile function accepts text or an abstract syntax tree and can produce a code object, which eval or exec can execute.[ref] These operations make translation and execution separately accessible without requiring them to live in different applications.
Just-in-time compilation gives another combination: an interactive implementation can translate a submitted program expression into machine code and then execute it immediately. The LLVM 20.1.0 tutorial demonstrates this by adding an intermediate representation module to its just-in-time compilation facility, looking up the generated function, and calling it.[ref] Thus calling a programming language permanently "compiled" or "interpreted" hides an implementation choice: ask which representation is translated, which is executed, and when.