Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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 ____.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
some-alt