On this page
types.CodeType
Prev: types.ClassMethodDescriptorType | Up: types | Next: types.coroutine
Rel: types.AsyncGeneratorType, types.BuiltinFunctionType, types.BuiltinMethodType, types.CapsuleType
Ref: docs.python.org — types.CodeType
Tags: #public #python3 #stdlib #types
Import
import types
from types import CodeType
Signature & Overview
types.CodeType(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, codestring, constants, names, varnames, filename, name, qualname, firstlineno, linetable, exceptiontable, freevars=(), cellvars=(), /)
Create a code object. Not for the faint of heart.
Methods & Attributes
CodeType.co_branches(self, /)—CodeType.co_branchesmethod/property.CodeType.co_lines(self, /)—CodeType.co_linesmethod/property.CodeType.co_positions(self, /)—CodeType.co_positionsmethod/property.CodeType.replace(self, /, **changes)— Return a copy of the code object with new values for the specified fields.
Usage
import types
code = (lambda x: x + 1).__code__
assert isinstance(code, types.CodeType) and code.co_varnames == ('x',)
Gotchas
- Use
code.replace(...)to derive modified code objects safely instead of callingCodeType(...)with positional args.
REPL Inspect
from types import CodeType
help(CodeType)
Linked from
Related notes
- tuts/python3/stdlib_module/types/ClassMethodDescriptorType Linked here; links to this page; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/coroutine Linked here; links to this page; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/AsyncGeneratorType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/BuiltinFunctionType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/BuiltinMethodType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/CapsuleType Linked here; shared tags: #stdlib, #types
- tuts/c/stdlib/stdint_stdbool_and_limits shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/CellType shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/CoroutineType shared tags: #stdlib, #types