Introduction to Python Metaclasses

Metaclasses are one of those Python features that sound complicated but are actually based on a simple idea:

Classes create objects.
Metaclasses create classes.

That’s it.

When you write:

class User:
pass

the class User is actually an …


This content originally appeared on DEV Community and was authored by Vafa

Metaclasses are one of those Python features that sound complicated but are actually based on a simple idea:

Classes create objects.
Metaclasses create classes.

That’s it.

When you write:

class User:
    pass

the class User is actually an object, and Python needs something to build that object.
By default, Python uses the metaclass called type:

print(type(User))

Output:

<class 'type'>

So type is the “class-maker.”

Why Would You Ever Use a Metaclass?

Most people never do — and that’s fine.

But frameworks like Django, SQLAlchemy, and Pydantic use metaclasses to:

add extra attributes to classes

validate or modify classes at creation time

automatically register subclasses

build features with less boilerplate

A metaclass lets you run logic when the class is created, not when it runs.

A Basic Metaclass Example

This metaclass prints the name of the class being created:

class LoggerMeta(type):
    def __new__(cls, name, bases, attrs):
        print("Creating:", name)
        return super().__new__(cls, name, bases, attrs)

Using it:

class Example(metaclass=LoggerMeta):
    pass

When the file loads, it prints:

Creating: Example

Because the metaclass runs before the class exists.

Adding Automatic Attributes

A more practical example:

class InfoMeta(type):
    def __new__(cls, name, bases, attrs):
        attrs["source"] = "auto"
        return super().__new__(cls, name, bases, attrs)

Usage:

class Product(metaclass=InfoMeta):
    pass

print(Product.source)

Output:

auto

The metaclass quietly added the attribute.

When to Use Metaclasses

Use them only when you need to control how classes are built — usually in libraries or frameworks.

Normal applications almost never need them.


This content originally appeared on DEV Community and was authored by Vafa


Print Share Comment Cite Upload Translate Updates
APA

Vafa | Sciencx (2025-11-21T08:10:07+00:00) Introduction to Python Metaclasses. Retrieved from https://www.scien.cx/2025/11/21/introduction-to-python-metaclasses/

MLA
" » Introduction to Python Metaclasses." Vafa | Sciencx - Friday November 21, 2025, https://www.scien.cx/2025/11/21/introduction-to-python-metaclasses/
HARVARD
Vafa | Sciencx Friday November 21, 2025 » Introduction to Python Metaclasses., viewed ,<https://www.scien.cx/2025/11/21/introduction-to-python-metaclasses/>
VANCOUVER
Vafa | Sciencx - » Introduction to Python Metaclasses. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/21/introduction-to-python-metaclasses/
CHICAGO
" » Introduction to Python Metaclasses." Vafa | Sciencx - Accessed . https://www.scien.cx/2025/11/21/introduction-to-python-metaclasses/
IEEE
" » Introduction to Python Metaclasses." Vafa | Sciencx [Online]. Available: https://www.scien.cx/2025/11/21/introduction-to-python-metaclasses/. [Accessed: ]
rf:citation
» Introduction to Python Metaclasses | Vafa | Sciencx | https://www.scien.cx/2025/11/21/introduction-to-python-metaclasses/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.