Quick Tip: What Is a Metaclass in Python?

This quick tip gives a brief overview of what we mean by a metaclass in Python and shows some examples of the concept.

Before delving into this article, I should point out an important point about classes in Python which makes it easier for us to grasp the concept of metaclasses.

Is a Class an Object in Python?!

If you've used a programming language other than Python, the concept you understood about classes is most likely that it is a way used to create new objects. This is also true in Python, but Python even takes it one more step further—classes are also considered objects!

So, if you created the following class in Python:

This simply means that an object with the name myClass has been created in memory. Since this object is able to create new objects, it is considered a class. This means we can apply object operations on classes in Python, as classes are objects themselves.

We can thus do operations on classes like assigning the class to a variable, as follows:

Which returns:

You can even pass the class myClass as a parameter to a method, as follows:

Which returns the following output:

In addition to other operations you can normally apply on objects.


Maybe you have come across the type keyword in Python? You most likely used it to check the type of some object, as shown in the following examples:

In which case you would get the following output:

Looking at the output, everything seems pretty clear until you come to the type type. To see what this might mean, let's go back to our class we defined at the beginning of this article:

Now, do the following:

What would be the output of this statement? It will surprisingly be:

So, we can conclude that the type of classes in Python is type!

What is the relation between a type and a metaclass? Well, a type is a metaclass, provided that the default metaclass is type. I know this might be confusing, especially that type can be used to return the class of some object as shown above, but this is due to the backward compatibility in Python. So, if you write:

You will get:

Meaning that a type is a type!

The term metaclass simply means something used to create classes. In other words, it is the class of a class, meaning that the instance of a class in this case is a class. Thus, type is considered a metaclass since the instance of a type is a class.

For instance, when we mentioned the following statement above:

This simply builds an object/instance of the class myClass. In other words, we used a class to create an object. 

In the same way, when we did the following:

The metaclass was used to create the class myClass (which is considered a type). So, like the object being an instance of a class, a class is an instance of a metaclass.

Using Metaclass to Create a Class

In this section, we are going to see how we can use a metaclass to create a class, rather than using the class statement as we saw in the classes and objects tutorial. As we saw above, the default metaclass is type. Thus, we can use the following statement to create a new class:

If you want to make things simpler, you can assign the same class name myClass to the variable name.

The dictionary { } here is used to define the attributes of the class. So, having the following statement:

Is similar to:

The __metaclass__ Attribute

Say that we created the class myClass as follows:

In this case, class creation will occur using myMetaClass instead of type, as follows:

Creation and Initialization of a Metaclass

If you want to have control on how you create and initialize a class after its creation, you can simply use the metaclass __new__ method and __init__ constructor, respectively. So, when myMetaClass above is called, this is what will be happening behind the scenes:

I don't want to make this quick tip longer, but to summarize, we can conclude that a metaclass is used in Python to create a class. That is, the instance of a metaclass is a class. If you want to delve deeper into metaclasses, you can check out this tutorial.



Related Articles