Python Metaclasses

The key concept of python is objects. Almost everything in python is an object, which includes functions and as well as classes. As a result, functions and classes can be passed as arguments, can exist as an instance, and so on. Above all, the concept of objects lets the classes in generating other classes.

The classes that generate other classes are defined as metaclasses. In this section, we will discuss the concept of metaclasses and the specific ways to use them. In this section, we will cover the following topics:

  • type
  • Writing Metaclasses
  • Metaclass Usecases


A class defines the properties and available actions of its object and also it acts as a factory for object creation. Let’s understand the process by creating a class using type directly. The exact class that is used for class instantiation is called type. Normally, we define a class using a special syntax called the class keyword, but this syntax is a substitute for type class. Let’s illustrate with an example:

 First of all, we look into the scenario of creating a class using the class keyword. Let’s check the below code:


Name: Hridaya, Age: 10, Gender: Male

Here we have created a class called Person using the class keyword. This class keyword acts as a substitute for type syntax. Now let’s look into, how to use type keyword. Let’s go through the below code:

Let’s focus on type. It has three arguments they are as follows:

  • The first argument is a string – Person. This string is assigned as the class name.
  • The second argument is a tuple – (object, ). This tells that the Person class inherits from the object class. Here, the trailing comma helps the python interpreter to recognize it as a tuple.
  • Here, the third argument is a dictionary that mentions the attribute of a class. In this case, the class has two methods – init and getDetails

Creating a Subclass using type

Let’s look into the normal scenario of creating a subclass, i.e., using the class keyword. Here, we will create a subclass Profession in which the main class is Person.


Name: Hridaya, Age: 10, Gender: Male
Profession: Programming

Now let’s see how to convert the above code using type.  For the Person class, the second argument of the type is the object class –  the superclass –, i.e., Person is the subclass of the Object class. Similarly, the Profession class is the subclass of Person, and hence while creating the Profession class, the second argument of type refers to the Person class. 


Name: Hridaya, Age: 10, Gender: Male
Profession: Programming

Writing Metaclasses

Metaclasses are classes that inherit directly from type. The method that custom metaclasses should implement is the __new__ method. The arguments mentioned in the __new__  method of metaclasses reflects in the __new__ method of type class. It has four positional arguments. They are as follows:

  1. The first argument is the metaclass itself.
  2. The second argument is the class name.
  3. The third argument is the  superclasses (in the form of tuples)
  4. The fourth argument is the attributes of class (in the form of a dictionary)

 Let’s have a look at the below code:


classname: A
superclasses: (<class 'object'>, )
attributedict: {}
class type: <class '__main__.MetaClass'>

You can note that the type of class A is a metaclass – MetaClass. Let’s check the type of a normal class.


<class 'type'>

Metaclass Inheritance

Let’s see, how to inherit from metaclasses.


<class '__main__.MetaClass'>

In this case, you can see that class B is an instance of a metaclass. This is because its superclass A is an instance of a metaclass. Now we will consider the scenario where class subclasses with two or more distinct classes. Let’s look into the below sample code:


Type of class A: <class '__main__.MetaClass'>
Type of class B: <class 'type'>
Type of class C: <class '__main__.MetaClass'>

Here, you can see class C is inherited from class A (metaclass) and class B ( type class). But here the type of class C is a metaclass. This is because when Python interpreter checked the superclasses, it found that the metaclass is a subclass of the type itself. So it considered metaclass as the type of class C to avoid any conflict.  

Let’s look into another sample code where a class inherits from two different metaclasses.

Here you will get the below error message while trying to inherit from two different metaclasses.

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) 
subclass of the metaclasses of all its bases

This is because Python can only have one metaclass for a class. Here, class C can’t inherit from two metaclasses, which results in ambiguity.

Post a Comment

Previous Post Next Post