Open In App

Python MetaClasses

Last Updated : 18 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Similar Reads

Metaprogramming with Metaclasses in Python
At first, the word Metaprogramming seems like a very funky and alien thing but if you have ever worked with decorators or metaclasses, you were doing metaprogramming there all along. In a nutshell, we can say metaprogramming is the code that manipulates code.In this article, we are going to discuss Metaclasses, why and when we should use them, and
8 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling_future_ modulePython Division operatorIf we are p
5 min read
Python program to build flashcard using class in Python
In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its
2 min read
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss certain ways in which this problem can be solved.
4 min read
Reading Python File-Like Objects from C | Python
Writing C extension code that consumes data from any Python file-like object (e.g., normal files, StringIO objects, etc.). read() method has to be repeatedly invoke to consume data on a file-like object and take steps to properly decode the resulting data. Given below is a C extension function that merely consumes all of the data on a file-like obj
3 min read
Python | Add Logging to a Python Script
In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', level = logging.ERROR) # Variables (to make the calls that follo
2 min read
Python | Add Logging to Python Libraries
In this article, we will learn how to add a logging capability to a library, but don’t want it to interfere with programs that don’t use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : C/C++ Code # abc.py import logging log = logging.getLogger(_
2 min read
JavaScript vs Python : Can Python Overtop JavaScript by 2020?
This is the Clash of the Titans!! And no...I am not talking about the Hollywood movie (don’t bother watching it...it's horrible!). I am talking about JavaScript and Python, two of the most popular programming languages in existence today. JavaScript is currently the most commonly used programming language (and has been for quite some time!) but now
5 min read
Python | Visualizing O(n) using Python
Introduction Algorithm complexity can be a difficult concept to grasp, even presented with compelling mathematical arguments. This article presents a tiny Python program that shows the relative complexity of several typical functions. It can be easily adapted to other functions. Complexity. Why it matters? Computational complexity is a venerable su
3 min read
Python | Index of Non-Zero elements in Python list
Sometimes, while working with python list, we can have a problem in which we need to find positions of all the integers other than 0. This can have application in day-day programming or competitive programming. Let's discuss a shorthand by which we can perform this particular task. Method : Using enumerate() + list comprehension This method can be
6 min read
Article Tags :
Practice Tags :