On this page
types
Prev: turtledemo | Up: stdlib_module | Next: typing
Rel: datetime, zoneinfo, calendar, collections, heapq
Ref: docs.python.org — types
Tags: #public #python3 #stdlib
Import
import types
Overview
Exposes names for built-in interpreter types that aren't in builtins (ModuleType, FunctionType, MethodType, UnionType, GenericAlias) plus two practical everyday utilities: SimpleNamespace (lightweight attribute bag) and MappingProxyType (read-only view over a dict).
Quick Start
from types import MappingProxyType, SimpleNamespace
cfg = SimpleNamespace(host="127.0.0.1", port=8000)
assert cfg.port == 8000
readonly = MappingProxyType({"env": "prod", "debug": False})
assert readonly["env"] == "prod"
Implementation Walkthrough
1. Exposing Immutable Public Registries with MappingProxyType
Wrap an internal dictionary in MappingProxyType so callers can read and iterate over it without mutating your internal state:
from types import MappingProxyType
_REGISTRY: dict[str, int] = {"alpha": 1, "beta": 2}
REGISTRY = MappingProxyType(_REGISTRY)
assert REGISTRY["alpha"] == 1
try:
REGISTRY["gamma"] = 3 # type: ignore[index]
except TypeError:
pass # read-only view rejects mutation
2. Lightweight Ad-Hoc Objects with SimpleNamespace
Great for quick test doubles and fixture objects without defining a full class:
from types import SimpleNamespace
fake_resp = SimpleNamespace(status=200, text="ok")
assert fake_resp.status == 200
Gotchas & Boundaries
MappingProxyTypeis a live view, not a frozen copy: Mutating the underlying_REGISTRYdict is immediately visible through the proxy, and nested mutable values (like lists inside the dict) remain mutable.- Prefer
@dataclass(frozen=True, slots=True)overSimpleNamespacefor production domain models where static typing and field validation matter.
Common Functions
- types.coroutine() —
coroutine(func)— Convert regular generator function to a coroutine. - types.get_original_bases() —
get_original_bases(cls, /)— Return the class's "original" bases prior to modification by__mro_entries__. - types.new_class() —
new_class(name, bases=(), kwds=None, exec_body=None)— Create a class object dynamically using the appropriate metaclass. - types.prepare_class() —
prepare_class(name, bases=(), kwds=None)— Call the prepare method of the appropriate metaclass. - types.resolve_bases() —
resolve_bases(bases)— Resolve MRO entries dynamically as specified by PEP 560.
Classes & Objects
- types.AsyncGeneratorType —
types.AsyncGeneratorTypeclass. (Methods:.aclose(),.asend(),.athrow()) - types.BuiltinFunctionType —
types.BuiltinFunctionTypeclass. - types.BuiltinMethodType —
types.BuiltinMethodTypeclass. - types.CapsuleType — Capsule objects let you wrap a C "void *" pointer in a Python object. They're a way of passing data through the Python interpreter without creating your own custom type.
- types.CellType — Create a new cell object.
- types.ClassMethodDescriptorType —
types.ClassMethodDescriptorTypeclass. - types.CodeType — Create a code object. Not for the faint of heart. (Methods:
.co_branches(),.co_lines(),.co_positions(),.replace()) - types.CoroutineType —
types.CoroutineTypeclass. (Methods:.close(),.send(),.throw()) - types.DynamicClassAttribute — Route attribute access on a class to getattr. (Methods:
.deleter(),.getter(),.setter()) - types.EllipsisType — The type of the Ellipsis singleton.
- types.FrameType —
types.FrameTypeclass. (Methods:.clear()) - types.FunctionType — Create a function object.
- types.GeneratorType —
types.GeneratorTypeclass. (Methods:.close(),.send(),.throw()) - types.GenericAlias — Represent a PEP 585 generic type For example, for t = list[int], t.origin is list and t.args is (int,).
- types.GetSetDescriptorType —
types.GetSetDescriptorTypeclass. - types.LambdaType — Create a function object.
- types.MappingProxyType — Read-only proxy of a mapping. (Methods:
.copy(),.get(),.items(),.keys(),.values()) - types.MemberDescriptorType —
types.MemberDescriptorTypeclass. - types.MethodDescriptorType —
types.MethodDescriptorTypeclass. - types.MethodType — Create a bound instance method object.
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/turtledemo
- tuts/python3/stdlib_module/types/AsyncGeneratorType
- tuts/python3/stdlib_module/types/BuiltinFunctionType
- tuts/python3/stdlib_module/types/BuiltinMethodType
- tuts/python3/stdlib_module/types/CapsuleType
- tuts/python3/stdlib_module/types/CellType
- tuts/python3/stdlib_module/types/ClassMethodDescriptorType
- tuts/python3/stdlib_module/types/CodeType
- tuts/python3/stdlib_module/types/coroutine
- tuts/python3/stdlib_module/types/CoroutineType
- tuts/python3/stdlib_module/types/DynamicClassAttribute
- tuts/python3/stdlib_module/types/EllipsisType
- tuts/python3/stdlib_module/types/FrameType
- tuts/python3/stdlib_module/types/FunctionType
- tuts/python3/stdlib_module/types/GeneratorType
- tuts/python3/stdlib_module/types/GenericAlias
- tuts/python3/stdlib_module/types/get_original_bases
- tuts/python3/stdlib_module/types/GetSetDescriptorType
- tuts/python3/stdlib_module/types/LambdaType
- tuts/python3/stdlib_module/types/MappingProxyType
- tuts/python3/stdlib_module/types/MemberDescriptorType
- tuts/python3/stdlib_module/types/MethodDescriptorType
- tuts/python3/stdlib_module/types/MethodType
- tuts/python3/stdlib_module/types/new_class
- tuts/python3/stdlib_module/types/prepare_class
- tuts/python3/stdlib_module/types/resolve_bases
- tuts/python3/stdlib_module/typing
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/turtledemo Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/AsyncGeneratorType Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/BuiltinFunctionType Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/BuiltinMethodType Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/CapsuleType Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/CellType Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/ClassMethodDescriptorType Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/CodeType Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/coroutine Linked here; links to this page; shared tags: #stdlib