On this page
types.new_class
Prev: types.MethodType | Up: types | Next: types.prepare_class
Rel: types.AsyncGeneratorType, types.BuiltinFunctionType, types.BuiltinMethodType, types.CapsuleType
Ref: docs.python.org — types.new_class
Tags: #public #python3 #stdlib #types
Import
import types
from types import new_class
Signature & Overview
types.new_class(name, bases=(), kwds=None, exec_body=None)
Create a class object dynamically using the appropriate metaclass.
Usage
import types
Dynamic = types.new_class('Dynamic', (object,), {}, lambda ns: ns.update(x=42))
assert Dynamic.x == 42
Gotchas
- Preferred over 3-arg
type()when base classes use__init_subclass__or custom__prepare__metaclasses.
REPL Inspect
from types import new_class
help(new_class)
Linked from
Related notes
- tuts/python3/stdlib_module/types/MethodType Linked here; links to this page; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/prepare_class Linked here; links to this page; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/types/AsyncGeneratorType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/BuiltinFunctionType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/BuiltinMethodType Linked here; shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/CapsuleType Linked here; shared tags: #stdlib, #types
- tuts/c/stdlib/stdint_stdbool_and_limits shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/CellType shared tags: #stdlib, #types
- tuts/python3/stdlib_module/types/ClassMethodDescriptorType shared tags: #stdlib, #types