Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Defining a Custom Metaclass | Custom Metaclasses in Action
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 ____.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What are some practical use cases for custom metaclasses?

Can you explain the difference between `__new__` and `__init__` in metaclasses?

How does using a metaclass affect class inheritance?

bookDefining a Custom Metaclass

Swipe to show 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 ____.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt