Python metaclasses have a reputation for being deep black magic. Like many aspects of Python, once you get them they're really quite straightforward.

Metaclasses aren't often useful, but when they are they can simplify complex code.

This article explains the basics of metaclasses by creating a metaclass that applies a decorator to all methods in a class. This could be for something useful, like profiling, but instead it's going to apply the selfless decorator from selfless Python. This makes the self implicit in method declarations.

To follow the example you'll need the module Byteplay.

There are lots more you can do with metaclasses. Uses include profiling, registering classes and dynamically creating classes based on external specs. like database schema. For a good article on the details and usage of metaclasses, see Python Metaclasses: Who? Why? When? (pdf) by Richard Jones .