Metaclasses in Python

In Python, everything is an object, even the classes that create objects. You used a class to instantiate an object instance, but classes themselves are also instantiated behind the scenes. This mechanism is available to you as a programmer through the concept of metaclasses.

Metaclasses allow you to hook when classes are created, and this technique is what’s behind magical frameworks like Django and SQLAlchemy where class definitions have side effects that impact databases.

In this course you’ll learn about how:

  • Everything really is an object, including classes themselves
  • The type() function works
  • Classes instantiate objects and get instantiated themselves
  • You can hook class instantiation with the metaclass argument

Metaclasses aren’t for the faint of heart, but they do allow you to do some magic. Plus, learning about them can help you understand what exactly happens behind the scenes when you instantiate a class.

What’s Included:

Downloadable Resources:

Related Learning Paths:

About Christopher Trudeau

Christopher has a passion for the Python language and writes for Real Python. He is a consultant who helps advise organizations on how to improve their technical teams.

» More about Christopher

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

← Browse All Courses