Metaclasses in Python

Metaclasses take the mantra “everything is an object” to the next level. One of Python's fascinating lesser-known features, they allow you to intervene on the creation of classes themselves, and not just their instances. So in other words, metaclasses are like classes for classes, but when would you ever need them?

While it is rare to genuinely need a metaclass, they are a fun feature to know about and play with. In this talk for SF Python, Jess Hamrick explains how metaclasses work, why you might want to use them, and provides examples from her own adventures using metaclasses to rewrite method docstings to be prefixed with the name of the class they belong to.

To continuing diving into Python, browse our stream of free Python resources, or check out our list of upcoming Python trainings.

Published January 11, 2014