Metaclasses allow you to deeply modify the behaviour of Python classes (in terms of how they're defined, instantiated, accessed, and more) by replacing the
type metaclass that new classes use by default.
When designing your architecture, consider that many things which can be accomplished with metaclasses can also be accomplished using more simple semantics:
- Traditional inheritance is often more than enough.
- Class decorators can mix-in functionality into a classes on a ad-hoc approach.
- Python 3.6 introduces
__init_subclass__()which allows a class to partake in the creation of its subclass.
type is called with three arguments it behaves as the (meta)class it is, and creates a new instance, ie. it produces a new class/type.
It is possible to subclass
type to create an custom metaclass.
Now, we have a new custom
mytype metaclass which can be used to create classes in the same manner as
When we create a new class using the
class keyword the metaclass is by default chosen based on upon the baseclasses.
In the above example the only baseclass is
object so our metaclass will be the type of
object, which is
type. It is possible override the default, however it depends on whether we use Python 2 or Python 3:
A special class-level attribute
__metaclass__ can be used to specify the metaclass.
metaclass keyword argument specify the metaclass.
Any keyword arguments (except
metaclass) in the class declaration will be passed to the metaclass. Thus
class MyDummy(metaclass=mytype, x=2) will pass
x=2 as a keyword argument to the
Read this in-depth description of python meta-classes for more details.
Custom functionality with metaclasses
Functionality in metaclasses can be changed so that whenever a class is built, a string is printed to standard output, or an exception is thrown. This metaclass will print the name of the class being built.
You can use the metaclass like so:
The standard output will be:
Introduction to Metaclasses
What is a metaclass?
In Python, everything is an object: integers, strings, lists, even functions and classes themselves are objects. And every object is an instance of a class.
To check the class of an object x, one can call
Most classes in python are instances of
type itself is also a class. Such classes whose instances are also classes are called metaclasses.
The Simplest Metaclass
OK, so there is already one metaclass in Python:
type. Can we create another one?
That does not add any functionality, but it is a new metaclass, see that MyClass is now an instance of SimplestMetaclass:
A Metaclass which does Something
A metaclass which does something usually overrides
__new__, to modify some properties of the class to be created, before calling the original
__new__ which creates the class:
Singletons using metaclasses
A singleton is a pattern that restricts the instantiation of a class to one instance/object. For more info on python singleton design patterns, see here.
The default metaclass
You may have heard that everything in Python is an object. It is true, and all objects have a class:
The literal 1 is an instance of
int. Lets declare a class:
Now lets instantiate it:
What is the class of
bar is an instance of
Foo. But what is the class of
Foo itself is an instance of
type. How about
So what is a metaclass? For now lets pretend it is just a fancy name for the class of a class. Takeaways:
- Everything is an object in Python, so everything has a class
- The class of a class is called a metaclass
- The default metaclass is
type, and by far it is the most common metaclass
But why should you know about metaclasses? Well, Python itself is quite "hackable", and the concept of metaclass is important if you are doing advanced stuff like meta-programming or if you want to control how your classes are initialized.