types — Dynamic type creation and names for built-in types¶
Source code: Lib/types.py
This module defines utility functions to assist in dynamic creation of new types.
It also defines names for some object types that are used by the standard
Python interpreter, but not exposed as builtins like int or
str are.
Finally, it provides some additional type-related utility classes and functions that are not fundamental enough to be builtins.
Dynamic Type Creation¶
- types.new_class(name, bases=(), kwds=None, exec_body=None)¶
Creates a class object dynamically using the appropriate metaclass.
The first three arguments are the components that make up a class definition header: the class name, the base classes (in order), the keyword arguments (such as
metaclass).The exec_body argument is a callback that is used to populate the freshly created class namespace. It should accept the class namespace as its sole argument and update the namespace directly with the class contents. If no callback is provided, it has the same effect as passing in
lambda ns: None.Added in version 3.3.
- types.prepare_class(name, bases=(), kwds=None)¶
Calculates the appropriate metaclass and creates the class namespace.
The arguments are the components that make up a class definition header: the class name, the base classes (in order) and the keyword arguments (such as
metaclass).The return value is a 3-tuple:
metaclass, namespace, kwdsmetaclass is the appropriate metaclass, namespace is the prepared class namespace and kwds is an updated copy of the passed in kwds argument with any
'metaclass'entry removed. If no kwds argument is passed in, this will be an empty dict.Added in version 3.3.
Changed in version 3.6: The default value for the
namespaceelement of the returned tuple has changed. Now an insertion-order-preserving mapping is used when the metaclass does not have a__prepare__method.
See also
- Metaclasses
Full details of the class creation process supported by these functions
- PEP 3115 - Metaclasses in Python 3000
Introduced the
__prepare__namespace hook
- types.resolve_bases(bases)¶
Resolve MRO entries dynamically as specified by PEP 560.
This function looks for items in bases that are not instances of
type, and returns a tuple where each such object that has an__mro_entries__()method is replaced with an unpacked result of calling this method. If a bases item is an instance oftype, or it doesn’t have an__mro_entries__()method, then it is included in the return tuple unchanged.Added in version 3.7.
- types.get_original_bases(cls, /)¶
Return the tuple of objects originally given as the bases of cls before the
__mro_entries__()method has been called on any bases (following the mechanisms laid out in PEP 560). This is useful for introspecting Generics.For classes that have an
__orig_bases__attribute, this function returns the value ofcls.__orig_bases__. For classes without the__orig_bases__attribute,cls.__bases__is returned.Examples:
from typing import TypeVar, Generic, NamedTuple, TypedDict T = TypeVar("T") class Foo(Generic[T]): ... class Bar(Foo[int], float): ... class Baz(list[str]): ... Eggs = NamedTuple("Eggs", [("a", int), ("b", str)]) Spam = TypedDict("Spam", {"a": int, "b": str}) assert Bar.__bases__ == (Foo, float) assert get_original_bases(Bar) == (Foo[int], float) assert Baz.__bases__ == (list,) assert get_original_bases(Baz) == (list[str],) assert Eggs.__bases__ == (tuple,) assert get_original_bases(Eggs) == (NamedTuple,) assert Spam.__bases__ == (dict,) assert get_original_bases(