Defining a Custom Metaclass
To create your own metaclass in Python, you subclass the built-in type. A metaclass is itself a class whose instances are classes, not regular objects. By defining a custom metaclass, you can control how classes are constructed, modify class attributes, or add custom logic during class creation. This approach is powerful when you need to enforce coding standards, register classes automatically, or inject behavior into all subclasses.
1234class PrintOnCreate(type): def __new__(mcs, name, bases, namespace): print(f"Creating class {name}") return super().__new__(mcs, name, bases, namespace)
12class MyClass(metaclass=PrintOnCreate): pass
When you define a metaclass, two special methods play a key role: __new__ and __init__. The __new__ method is responsible for actually creating the class object. It receives the metaclass itself, the name of the class being created, its base classes, and the class body as a dictionary. You can modify the class attributes or even return a completely different class object here. After __new__ completes, the __init__ method is called to initialize the newly created class. This is where you can perform additional setup or validation after the class object is fully constructed.
1. Which method is called first during class creation in a metaclass?
2. How do you specify a metaclass for a class in Python 3?
3. Fill in the blank: To define a metaclass, subclass ____.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 4.76
Defining a Custom Metaclass
Svep för att visa menyn
To create your own metaclass in Python, you subclass the built-in type. A metaclass is itself a class whose instances are classes, not regular objects. By defining a custom metaclass, you can control how classes are constructed, modify class attributes, or add custom logic during class creation. This approach is powerful when you need to enforce coding standards, register classes automatically, or inject behavior into all subclasses.
1234class PrintOnCreate(type): def __new__(mcs, name, bases, namespace): print(f"Creating class {name}") return super().__new__(mcs, name, bases, namespace)
12class MyClass(metaclass=PrintOnCreate): pass
When you define a metaclass, two special methods play a key role: __new__ and __init__. The __new__ method is responsible for actually creating the class object. It receives the metaclass itself, the name of the class being created, its base classes, and the class body as a dictionary. You can modify the class attributes or even return a completely different class object here. After __new__ completes, the __init__ method is called to initialize the newly created class. This is where you can perform additional setup or validation after the class object is fully constructed.
1. Which method is called first during class creation in a metaclass?
2. How do you specify a metaclass for a class in Python 3?
3. Fill in the blank: To define a metaclass, subclass ____.
Tack för dina kommentarer!