Python metaclass With Examples

Welcome to today’s tutorial on python metaclass. We all know that python is an object oriented programming language which means it binds datas with functionalities i.e. class. Before starting about python metaclass we hope you know the concept of class and inheritance. If you don’t have the idea, then you can learn python class and python inheritance first.

Python Metaclass

Python metaclass is a class that instantiates a class. In python, a class is actually an object of another class. This another class is called metaclass. Metaclass in python defines the behaviors of class objects. let’s look at some examples to understand the metaclass concept clearly.

Python built-in Metaclass

type is a built in metaclass in python. Let’s start with the following example:

In the above code we have created a class named SampleClass, and creating an object of it. Then type of object says:

That means obj is an object of SampleClass. Now if we try to see the type of SampleClass itself as following:

Then the output is:

That means SampleClass is an object of class type. To be more specific SampleClass is an instance of class type. So type is a metaclass. Every class of python belongs to the built-in metaclass type.

How python metaclass works?

Whenever we create any class then the default metaclass gets called. This metaclass gets called with three information – name of the class, set of base classes of the class and the attributes of the class.

As type is the builtin metaclass, whenever we create a class type gets called with these three argument.

We are saying that every class in python is also a object of type, which means we can create any class in a single line just like we create object of any class. This way of creating a class is called “creating class on the fly”.

Create python Class using metaclass

So using the metaclass type, you can create your class in a single line by calling as following:

This will create a class named Student at run time of the code. The above line is equivalent to the following code:

If inherits some other class say for example Department then we write as following,

Inherited classes should be provided in the second argument when creating class on the fly,

If Student class contains some attributes and functions then they should be provided in the 3rd argument as key value pair. See following the examples:

Those attributes and functions can be added as following:

Notice that we have defined the function before we use it. One more thing I want you to understand is that the first argument is class name. So if you write as following:

Then the output will be,

So, it’s better to keep the same name for class and variable to keep consistency.

Set the metaclass

You can set your class’s metaclass explicitly. Whenever python gets the keyword class then it searches for the metaclass. If not found then the default metaclass type is used to create the object of the class. You can set metaclass of your class using the __metaclass__ attribute as following:

It will produce output as;

Creating Metaclass in Python

Finally, you can also create your own metaclass to define the behaviour of any class that are created using your class.

To do that your class must inherit the default metaclass type as this is the main metaclass. See the following example:


It will output:


Python metaclass is a very complicated topic. Here we have given the basic idea of metaclass. Hope now, you do understand the basic concept of metaclass. To understand the functionalities of metaclass you have to go very deep. However you should be aware of how python classes work and what is the role of metaclass in python.

By admin

Leave a Reply

%d bloggers like this: