type is the most unique construct in Python. It is both a class and an object.
- It is an instance of itself:
type(type) == type - It inherits from object:
issubclass(type, object) == True objectis an instance oftype:type(object) == type
This creates the core foundation of Python's object model: everything inherits from object, but everything is built by type.
When CPython reads a class definition during Evaluation Time, it executes a highly specific sequence:
class MyModel(BaseModel, metaclass=CustomMeta):
data = 5
def do_work(self): pass- Metaclass Discovery: Python looks for
metaclass=in the class signature. If none exists, it checks the parent classes. If none exist, it defaults totype. - Prepare Namespace: Python calls
CustomMeta.__prepare__(name, bases). This returns an empty dictionary. (In advanced cases, it returns anOrderedDictto preserve attribute declaration order). - Evaluate Body: Python executes the code inside the class block.
data = 5and thedo_workfunction are loaded into the dictionary returned by Step 2. - Metaclass
__new__: Python callsCustomMeta.__new__(CustomMeta, "MyModel", (BaseModel,), dict_from_step_3). This physically allocates the memory for the new Class Object. __init_subclass__: After the class is fully built, Python scans its parent classes (BaseModel) and calls their__init_subclass__methods, allowing the parent to react to the newly born child.
A Class Decorator executes after Step 5.
@add_timestamp
class MyModel: pass
# Under the hood, this is literally just:
MyModel = add_timestamp(MyModel)Because it happens after Step 5, you cannot intercept or prevent the creation of the class. If the class definition was invalid, the memory was already allocated and the time was already wasted. A Metaclass (Step 4) intercepts the process before the memory is allocated.