Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Metaclasses in Python (Overview)

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.

Download

Sample Code (ZIP)

21.2 KB
Download

Course Slides (PDF)

1.0 MB

00:00 Welcome to Python Metaclasses. My name is Chris, and I will be your guide. This course is about a tiny little corner of Python, how the machinery behind classes work and how you can hook it.

00:11 This course is not for the faint of heart, and you could go your entire programming career without ever using any of it. If you’re curious about how classes get instantiated or are interested in writing frameworks where a lot of magical things happen behind the scenes, this course might be for you.

00:29 To get the full picture of a metaclass, first, you need to understand how everything—and I mean everything, including classes—in Python is an object, what the type() function does, how classes instantiate objects, how classes—which are objects—get instantiated, and how to hook that for fame and profit. All right, maybe not fame and profit.

00:53 How about esoteric code goodness instead?

00:58 This course was tested using Python 3.11.2. Most of the concepts introduced here have been around for a while. If you’re using Python 3.7 or later, you should be good.

01:09 Although metaclasses do exist in Python 2 after 2.2, the syntax for it is significantly different. I’ll be sticking with Python 3 throughout the course.

01:21 You’ll be tired of me saying it by the end of the course, but everything, including classes, in Python is an object. When you create a class, you are implicitly instantiating it, and the underlying mechanism used by Python to do this is available to you as a programmer. This can get a little tricky.

01:39 You’re creating things that create things, and that level of abstraction can warp the mind a little bit. Three cases where metaclasses are used are: A factory pattern, where the creation of an object is supposed to have a side effect, like registering it with a central store. Singletons, where you absolutely, positively only want a single instance of an object.

02:00 You can enforce that using a metaclass by controlling how the class instantiates objects. And frameworks like Django and SQLAlchemy that use metaclasses to perform the magic relationship between ORM object attributes and database tables. In fact, the Python standard library uses metaclasses to implement the Enum class.

02:21 Once I’ve covered the concept of metaclasses with you, I’ll walk you through Enum and its companion, the EnumType, to show you a real-world use case.

02:31 Before going all meta, you’ll need to understand the intricacies of classes and the type() function. That’s what’s next.

Become a Member to join the conversation.