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 |
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 |
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 |
co_flags
The bits below are those CPython 3.6 sets. 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 |
| 0x2000 | FUTURE_DIVISION | from __future__ import division |
| 0x4000 | FUTURE_ABSOLUTE_IMPORT | from __future__ import absolute_import |
| 0x8000 | FUTURE_WITH_STATEMENT | from __future__ import with_statement |
| 0x10000 | FUTURE_PRINT_FUNCTION | from __future__ import print_function |
| 0x20000 | FUTURE_UNICODE_LITERALS | from __future__ import unicode_literals |
| 0x40000 | FUTURE_BARRY_AS_BDFL | from __future__ import barry_as_FLUFL |
| 0x80000 | FUTURE_GENERATOR_STOP | from __future__ import generator_stop |