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
| Name | Type | Description |
|---|---|---|
| name | str | Name of the class to create or prepare. |
| bases | tuple | Original base class entries. |
| kwds | dict or None | Class-header keyword arguments, including an optional metaclass. |
| exec_body | callable or None | Callback 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)