Skip to content
Related Articles

Related Articles

Improve Article

Python MetaClasses

  • Last Updated : 18 Aug, 2020

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 let 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

type

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:

Python3




class FoodType(object):
  def __init__(self, ftype):
    self.ftype = ftype
      
  def getFtype(self):
    return self.ftype
    
  
def main():
  fType = FoodType(ftype = 'Vegetarian')
  print(fType.getFtype())
    
main()
Output



Vegetarian



Here we have created a class called FoodType 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:

Python3




def init(self, ftype):
    self.ftype = ftype
  
def getFtype(self):
    return self.ftype 
  
FoodType = type('FoodType', (object, ), {
    '__init__': init,
    'getFtype' : getFtype,
    })
  
fType = FoodType(ftype ='Vegetarian')
print(fType.getFtype())
Output
Vegetarian



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

  • The first argument is a string – FoodType. This string is assigned as the class name.
  • The second argument is a tuple – (object, ). This tells that the FoodType 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 getFtype.

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 VegType in which the main class is FoodType.

Python3




class FoodType(object):
  def __init__(self, ftype):
    self.ftype = ftype
      
  def getFtype(self):
    return self.ftype
    
class VegType(FoodType):
  def vegFoods(self):
    return {'Spinach', 'Bitter Guard'}
    
def main():
  vType = VegType(ftype = 'Vegetarian')
  print(vType.getFtype())
  print(vType.vegFoods())
    
main()
Output
Vegetarian
{'Spinach', 'Bitter Guard'}



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

Python3




def init(self, ftype):
    self.ftype = ftype
  
def getFtype(self):
    return self.ftype 
  
FoodType = type('FoodType', (object, ), {
    '__init__': init,
    'getFtype' : getFtype,
    })
  
def vegFoods(self):
    return {'Spinach', 'Bitter Guard'}
   
## creating subclass using type
VegType = type('VegType', (FoodType, ), {
    'vegFoods' : vegFoods,
    })
  
  
vType = VegType(ftype ='Vegetarian')
print(vType.getFtype())
print(vType.vegFoods())
Output
Vegetarian
{'Spinach', 'Bitter Guard'}



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 tuple)
  4. The fourth argument is the attributes of class (in the form of dictionary)

 Let’s have a look at the below code.

Python3




class MetaCls(type):
    """A sample metaclass without any functionality"""
    def __new__(cls, clsname, superclasses, attributedict):
        print("clsname:", clsname)
        print("superclasses:", superclasses)
        print("attrdict:", attributedict)
        return super(MetaCls, cls).__new__(cls, \
                       clsname, superclasses, attributedict)
  
C = MetaCls('C', (object, ), {})
print("class type:", type(C))
Output
clsname: C
superclasses: (<class 'object'>, )
attrdict: {}
class type: <class '__main__.MetaCls'>



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

Python3




class S(object):
  pass
  
print(type(S))
Output
<class 'type'>



Metaclass Inheritance

Let’s see, how to inherit from metaclasses.

Python3




class MetaCls(type):
    """A sample metaclass without any functionality"""
    def __new__(cls, clsname, supercls, attrdict):
          
        return super(MetaCls, cls).__new__(cls, clsname, supercls, attrdict)
  
C = MetaCls('C', (object, ), {})
## class A inherits from MetaCls       
class A(C):
  pass
  
print(type(A))
Output
<class '__main__.MetaCls'>



In this case, you can see that class A is an instance of a metaclass. This is because its superclass C 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:

Python3




class MetaCls(type):
    """A sample metaclass without any functionality"""
    def __new__(cls, clsname, supercls, attrdict):
          
        return super(MetaCls, cls).__new__(cls, clsname, supercls, attrdict)
  
## a class of the type metclass
A = MetaCls('A', (object, ), {})
print('Type of class A:', type(A))
  
class B(object):
    pass
print('Type of class B:', type(B))
  
## class C inherits from both the class, A and B
class C(A, B):
    pass
print('Type of class C:', type(C))
Output
Type of class A: <class '__main__.MetaCls'>
Type of class B: <class 'type'>
Type of class C: <class '__main__.MetaCls'>



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.



Python3




class MetaCls(type):
    """A sample metaclass without any functionality"""
    def __new__(cls, clsname, supercls, attrdict):
          
        return super(MetaCls, cls).__new__(cls, clsname, supercls, attrdict)
  
A = MetaCls('A', (object, ), {})
  
  
class NewMetaCls(type):
    """A sample metaclass without any functionality"""
    def __new__(cls, clsname, supercls, attrdict):
          
        return super(NewMetaCls, cls).__new__(cls, clsname, supercls, attrdict)
  
NewA = NewMetaCls('NewA', (object, ), {})
  
class C(A, NewA):
  pass

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

Traceback (most recent call last):
  File "/home/eb81e4ecb05868f83d5f375ffc78e237.py", line 18, in <module>
    class C(A, NewA):
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.

Metaclass Usecases

In most cases, we don’t need to go for a metaclass, normal code will fit with the class and object. The pointless use of metaclasses increases the complexity of coding. But there are scenarios where metaclass provides clear and efficient solutions. Let’s look into a few use cases. 

Class Verification

If you need to design a class that agrees to a particular interface, then a metaclass is the right solution. We can consider a sample code where a class requires either one of the attributes to be set. Let’s go through the code.

Python3




class MainClass(type):
    def __new__(cls, name, bases, attrs):
        if 'foo' in attrs and 'bar' in attrs:
            raise TypeError('Class % s cannot contain both foo and bar \
attributes.' % name)
        if 'foo' not in attrs and 'bar' not in attrs:
            raise TypeError('Class % s must provide either a foo \
attribute or a bar attribute.' % name)
        else:
          print('Success')
              
  
        return super(MainClass, cls).__new__(cls, name, bases, attrs)
  
class SubClass(metaclass = MainClass):
    foo = 42
    bar = 34
  
  
subCls = SubClass()

Here we tried to set two attributes. Hence, the design prevented it from setting and raised the below error. 

Traceback (most recent call last):
  File "/home/fe76a380911f384c4517f07a8de312a4.py", line 13, in <module>
    class SubClass(metaclass = MainClass):
  File "/home/fe76a380911f384c4517f07a8de312a4.py", line 5, in __new__
    attributes.' %name)
TypeError: Class SubClass cannot both foo and bar attributes.


You may think, using decorators you can easily make a class that agrees to a particular standard. But the disadvantage of using class decorator is that it must be explicitly applied to each subclass. 

Prevent  inheriting the attributes

A metaclass is an efficient tool to prevent sub class from inheriting certain class functions. This scenario can be best explained in the case of Abstract classes. While creating abstract classes, it is not required to run the functionality of the class. Let’s have a look at the below code.  

Python3




class MetaCls(type):
    def __new__(cls, name, bases, attrs):
        # If abstract class, then skip the metaclass function
        if attrs.pop('abstract', False):
            print('Abstract Class:', name)
            return super(MetaCls, cls).__new__(cls, name, bases, attrs)
          
        # metaclass functionality
        if 'foo' in attrs and 'bar' in attrs:
            raise TypeError('Class % s cannot contain both foo and bar \
attributes.' % name)
        if 'foo' not in attrs and 'bar' not in attrs:
            raise TypeError('Class % s must provide either a foo \
attribute or a bar attribute.' % name)
        print('Normal Class:', name)
        return super(MetaCls, cls).__new__(cls, name, bases, attrs)
  
class AbsCls(metaclass = MetaCls):
    abstract = True
      
  
class NormCls(metaclass = MetaCls):
    foo = 42
Output
Abstract Class: AbsCls
Normal Class: NormCls



Dynamic generation of classes

The dynamic generation of classes opens up a lot of possibilities. Let’s see how to generate classes dynamically using type.

Python3




class FoodType(object):
    events = []
  
    def __init__(self, ftype, items):
        self.ftype = ftype
        self.items = items
        FoodType.events.append(self)
  
    def run(self):
        print("Food Type: % s" %(self.ftype))
        print("Food Menu:", self.items) 
  
    @staticmethod
    def run_events():
        for e in FoodType.events:
            e.run()
  
def sub_food(ftype):
    class_name = ftype.capitalize()
    def __init__(self, items):
        FoodType.__init__(self, ftype, items)
    # dynamic class creation and defining it as a global attribute    
    globals()[class_name] = \
    type(class_name, (FoodType, ), dict(__init__ = __init__))
            
          
if __name__ == "__main__":
    foodType = ["Vegetarian", "Nonvegetarian"]
    foodItems = "Vegetarian(['Spinach', 'Bitter Guard']);\
Nonvegetarian(['Meat', 'Fish'])"
    # invoking method for dynamic class creation.
    [sub_food(ftype) for ftype in foodType]
    # executing dynamic classes.
    exec(foodItems)
    FoodType.run_events()
Output
Food Type: Vegetarian
Food Menu: ['Spinach', 'Bitter Guard']
Food Type: Nonvegetarian
Food Menu: ['Meat', 'Fish']



In this case, we create two subclasses – Vegetarian and NonVegetarian – dynamically, which inherits from the FoodType class.

Summary

Normal classes that are designed using class keyword have type as their metaclasses, and type is the primary metaclass. Metaclasses are a powerful tool in Python that can overcome many limitations. But most of the developers have a misconception that metaclasses are difficult to grasp.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :