Python Glossary Русский

all Python 3typesterm

types

module

Names for the interpreter's built-in types (functions, modules, generators) and helpers for creating classes dynamically.

Syntax

import types

The types module is used for checks like isinstance(f, types.FunctionType) and for two everyday classes: SimpleNamespace, an attribute bag without a class definition, and MappingProxyType, a read-only dictionary. Do not confuse it with typing: types describes real objects at run time, while typing provides hints for static checkers. For a simple callability test the built-in callable() is easier than listing function types.

Examples

import types
ns = types.SimpleNamespace(x=1, y=2)
print(ns.x + ns.y, ns)  # → 3 namespace(x=1, y=2)
print(isinstance(len, types.BuiltinFunctionType))  # → True
ro = types.MappingProxyType({"a": 1})
ro["a"] = 2  # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy