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. Dynamic typing 2026-11-05
With dynamic typing, the types of values determine the operations permitted during execution, while a variable name can be rebound to values of different types. This differs from changing the type of one existing value. In Python,
x=3; x="three" rebinds the name; the integer object has not become a string.[ref]Python 3.14 Data Model. §3.1, Objects, values and types
A call can succeed for several classes if each supplies the operations the function actually uses. This is often called duck typing: a function iterating over its argument can accept different iterable objects. An unsupported operation still fails when reached. Dynamic typing does not imply absence of a type system, automatic conversion between every pair of types, or that every error waits until execution; syntax errors and some other checks can occur earlier.
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.