Runtime Utility Functions

types.new_class()

Create a class dynamically, selecting the metaclass and preparing the namespace. The optional callback populates that namespace; it is not a string of source code.

Formal Syntax & Structure

python syntaxpython
types.new_class(name, bases=(), kwds=None, exec_body=None)

Parameters & Attributes

NameTypeDescription
namestrName of the class to create or prepare.
basestupleOriginal base class entries.
kwdsdict or NoneClass-header keyword arguments, including an optional metaclass.
exec_bodycallable or NoneCallback that populates the prepared namespace.

Return Value

type

The type result described above.

Working Example

Example Snippetpython
import typesC=types.new_class("C", (), {}, lambda ns: ns.update(value=7))print(C.__name__, C().value)
Python Reference