types — names for built-in types¶
This module defines names for some object types that are used by the standard Python interpreter but are not exposed as builtins (such as the type of functions or generators), together with a small number of helpers for dynamic class construction.
This is a MicroPython port of a subset of CPython’s types module. Several
type aliases that have no useful representation in MicroPython (CodeType,
MappingProxyType, SimpleNamespace, TracebackType, FrameType,
GetSetDescriptorType, MemberDescriptorType) are exposed as None
placeholders so that code which only references them keeps importing, but they
will not match any real object via isinstance().
Availability: This module is part of micropython-lib. It is
included by default only on the OpenMV N6 and OpenMV AE3. On the other
OpenMV Cams it can be installed with mip (or frozen into a custom
firmware); see Package management.
Type aliases¶
- types.FunctionType: type¶
The type of user-defined functions and of objects created by
defstatements.
- types.LambdaType: type¶
The type of
lambdaexpressions. Identical toFunctionType.
- types.BuiltinFunctionType: type¶
The type of built-in functions such as
len()andsys.exit().
- types.BuiltinMethodType: type¶
The type of bound methods of built-in types (for example
[].append). Identical toBuiltinFunctionType.
- types.CodeType: None¶
Placeholder for the CPython
codeobject type. AlwaysNonein this implementation.
- types.MappingProxyType: None¶
Placeholder for the read-only mapping proxy type returned by
type.__dict__in CPython. AlwaysNonein this implementation.
- types.SimpleNamespace: None¶
Placeholder for the CPython
types.SimpleNamespaceclass. AlwaysNonein this implementation.
- types.TracebackType: None¶
Placeholder for the type of traceback objects. Always
Nonein this implementation.
- types.FrameType: None¶
Placeholder for the type of frame objects. Always
Nonein this implementation.
Functions¶
- types.new_class(name: str, bases: tuple = (), kwds: dict | None = None, exec_body: Callable[[dict], None] | None = None) type¶
Create a class object dynamically in a way that mirrors the PEP 3115-compliant
classstatement.name is the name of the new class.
bases is a tuple of base classes.
kwds is a dict of keyword arguments to pass to the metaclass. A
"metaclass"key, if present, selects the metaclass directly.exec_body is an optional callable that will be invoked with the freshly prepared class namespace; it should populate it with the new class’s attributes.
Returns the newly constructed class.
- types.prepare_class(name: str, bases: tuple = (), kwds: dict | None = None) tuple[type, dict, dict]¶
Compute the appropriate metaclass and prepare the namespace for a new class.
name is the name of the class about to be created.
bases is a tuple of base classes.
kwds is a dict of keyword arguments. A
"metaclass"key, if present, is removed from the returned kwds and used as the metaclass. Otherwise the metaclass ofbases[0]is used, falling back totype.
Returns a 3-tuple
(metaclass, namespace, kwds)where namespace is the result of callingmetaclass.__prepare__if defined, or an empty dict otherwise, and kwds is a copy of the input with any"metaclass"entry removed.