Python Glossary Русский

Python 3.3+typesfunction

types.prepare_class()

dynamic class creation

Performs the first step of class creation: picks the metaclass and prepares the namespace via __prepare__. Returns a triple (metaclass, ns, kwds).

Syntax

meta, ns, kwds = types.prepare_class(name, bases=(), kwds=None)

It helps when you assemble a class by hand and want to know which metaclass wins: Python takes the most derived of the bases' metaclasses and an explicit metaclass=, and incompatible ones raise TypeError. The metaclass key is already removed from kwds — the rest goes to the metaclass. Usually new_class is enough, as it calls prepare_class itself.

Examples

# Python 3.11+
import enum
import types
meta, ns, kwds = types.prepare_class('Color', (enum.Enum,))
print(meta.__name__, isinstance(ns, dict), kwds)  # → EnumType True {}

See also

Open in the full glossarydocs.python.orgReport an inaccuracy