· Hakan Çelik · Python / Metaclass · 1 dk okuma

Dynamic Class Creation

Writing `class Example: ...` and calling `type('Example', (), {...})` produce exactly the same result. Python transforms the class keyword into a type() call behind the scenes.

Dynamic Class Creation

The class keyword is merely syntactic sugar. Python transforms it behind the scenes into a type(name, bases, namespace) call. The following two definitions produce exactly the same class:

class Example:
	attr = 1

	def method(self):
		return "method"

name = "Example"
bases = ()
namespace = {
	"attr": 1,
	"method": lambda self: "method"
}

Example = type(name, bases, namespace)

print(f"{Example.__class__=}")     # <class 'type'>
print(f"{Example().attr=}")        # 1
print(f"{Example().method()=}")    # 'method'
assert isinstance(Example, type)
assert isinstance(Example(), Example)

The Three Arguments

In the type(name, bases, namespace) call:

  • name — the name of the class (written to the __name__ attribute).
  • bases — a tuple of inherited classes; an empty tuple means deriving from object.
  • namespace — a dictionary containing all assignments in the class body: attributes, methods, __module__, __qualname__, etc.

Why Does This Matter?

Dynamic class creation is useful when you need to determine class names or attributes at runtime — for example, when an ORM generates table definitions from code, or when a plugin system registers plugins. This is also the first step to understanding metaclasses: a metaclass is a class that takes the place of type and controls this type(name, bases, namespace) call.

Back to Blog

Related Posts

View All Posts »
Understanding Python Classes

Understanding Python Classes

Python · 2 dk

In Python, everything is an object and every object has a type — including primitives, functions, and classes themselves. type() and __class__ reveal this relationship.

Run Methods Order In Python

Run Methods Order In Python

Python · 2 dk

Which method runs when in Python metaclasses? The execution order of __prepare__, __new__, __init__, and __call__ during class definition and instance creation.