A code object is the compiled body of a module, class, function or comprehension. In the
file it is a c
marshal object whose fields follow one another with no field names. A nested function or
class is a further code object among the constants of the one that defines it. The
numbers are 32-bit little-endian, and the other fields are marshal objects.
Fields, in file order
| Field | Stored as | Meaning |
|---|---|---|
co_argcount | 32-bit number | How many positional parameters, positional-only ones included |
co_posonlyargcount | 32-bit number | How many of them are positional-only. Not present in files of magic 3400 and 3401, from before it was added at 3410 |
co_kwonlyargcount | 32-bit number | How many keyword-only parameters |
co_nlocals | 32-bit number | How many local variables, parameters included |
co_stacksize | 32-bit number | The most values the code needs on the stack at one time |
co_flags | 32-bit number | Facts about the code, as bits |
co_code | bytes | The instructions |
co_consts | tuple | The constants the code loads, nested code objects among them. In a function with a docstring, the docstring is the first |
co_names | tuple of strings | Names of globals, attributes and imports used by the instructions |
co_varnames | tuple of strings | Local variable names, parameters first |
co_freevars | tuple of strings | Names taken from enclosing functions |
co_cellvars | tuple of strings | Local names that inner functions take; cell and free variable instructions number these first, then co_freevars |
co_filename | string | The source file’s path as compiled |
co_name | string | The name of the function or class; <module> for a module, <listcomp> and the like for others |
co_firstlineno | 32-bit number | The source line the code starts at |
co_lnotab | bytes | The line number table |
For example, CPython 3.9 compiles def f(a, /, b): return a to a code
object with co_argcount 2, co_posonlyargcount 1,
co_kwonlyargcount 0, co_nlocals 2,
co_stacksize 1, co_flags 0x43,
co_varnames ('a', 'b') and co_code
7C 00 53 00.
co_flags
The bits below are those CPython 3.8 and 3.9 set. Other bits are not used.
| Bit | Name | Meaning |
|---|---|---|
| 0x0001 | OPTIMIZED | Locals are held in fast slots |
| 0x0002 | NEWLOCALS | A new local namespace is made for each call |
| 0x0004 | VARARGS | Has a *args parameter |
| 0x0008 | VARKEYWORDS | Has a **kwargs parameter |
| 0x0010 | NESTED | A function nested in another function |
| 0x0020 | GENERATOR | A generator function |
| 0x0040 | NOFREE | No free or cell variables |
| 0x0080 | COROUTINE | An async def coroutine |
| 0x0100 | ITERABLE_COROUTINE | A generator that may be awaited, as made by types.coroutine |
| 0x0200 | ASYNC_GENERATOR | An asynchronous generator |
Other bits record the __future__ imports in force when the code was
compiled. Those bits moved partway through the 3.8 series, to stop them colliding with
compiler flags (bpo-39562). Releases 3.8.0, 3.8.1 and 3.8.2 use the first column, and
3.8.3 onwards and all of 3.9 use the second. Both groups of 3.8 files have magic number
3413, so a file does not say which it is.
| Name | 3.8.0 to 3.8.2 | 3.8.3 on, 3.9 | __future__ feature |
|---|---|---|---|
FUTURE_DIVISION | 0x2000 | 0x20000 | division |
FUTURE_ABSOLUTE_IMPORT | 0x4000 | 0x40000 | absolute_import |
FUTURE_WITH_STATEMENT | 0x8000 | 0x80000 | with_statement |
FUTURE_PRINT_FUNCTION | 0x10000 | 0x100000 | print_function |
FUTURE_UNICODE_LITERALS | 0x20000 | 0x200000 | unicode_literals |
FUTURE_BARRY_AS_BDFL | 0x40000 | 0x400000 | barry_as_FLUFL |
FUTURE_GENERATOR_STOP | 0x80000 | 0x800000 | generator_stop |
FUTURE_ANNOTATIONS | 0x100000 | 0x1000000 | annotations |
The explorer names these bits by the second column for every 3.8 and 3.9 file. A file from 3.8.0 to 3.8.2 that sets one of them shows it under a different name or as an unnamed bit.