Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Defining a Custom Metaclass | Custom Metaclasses in Action
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Metaclasses Demystified

bookDefining 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.

1234
class PrintOnCreate(type): def __new__(mcs, name, bases, namespace): print(f"Creating class {name}") return super().__new__(mcs, name, bases, namespace)
copy
12
class MyClass(metaclass=PrintOnCreate): pass
copy

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 ____.

question mark

Which method is called first during class creation in a metaclass?

Select the correct answer

question mark

How do you specify a metaclass for a class in Python 3?

Select the correct answer

question-icon

Fill in the blank: To define a metaclass, subclass ____.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookDefining a Custom Metaclass

Glissez pour afficher le menu

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.

1234
class PrintOnCreate(type): def __new__(mcs, name, bases, namespace): print(f"Creating class {name}") return super().__new__(mcs, name, bases, namespace)
copy
12
class MyClass(metaclass=PrintOnCreate): pass
copy

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 ____.

question mark

Which method is called first during class creation in a metaclass?

Select the correct answer

question mark

How do you specify a metaclass for a class in Python 3?

Select the correct answer

question-icon

Fill in the blank: To define a metaclass, subclass ____.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1
some-alt