Home » Python

metaclass in Python

Python metaclass: Here, we are going to learn about the metaclass in Python, defining metaclass in Python, creating singleton class using metaclass in Python.
Submitted by Sapna Deraje Radhakrishna, on November 02, 2019

Python metaclass

A metaclass is the class of a class. A class defines how an instance of a class i.e.; an object behaves whilst a metaclass defines how a class behaves. A class is an instance of a metaclass.

A metaclass is most commonly used as a class-factory. While an object is created by calling the class, Python creates a new class (when it executes the 'class' statement) by calling the metaclass. Combined with the __init__ and __new__ methods, metaclasses allows implementing 'additional things' when creating a class, like replace the class with something else entirely.

When the class statement is executed, Python first executes the body of the class statement as a normal block of code. The resulting namespace (a dict) holds the attributes of the class-to-be. The metaclass is determined by looking at the base classes of the class-to-be (metaclasses are inherited), at the __metaclass__ attribute of the class-to-be (if any) or the __metaclass__ global variable. The metaclass is then called with the name, bases, and attributes of the class to instantiate it.

The usecases of metaclass can be numerous, like

  1. Logging and profiling
  2. Dynamically adding new methods
  3. Dynamic resource locking/synchronization
  4. Registering the class at creation

Defining Metaclass

Metaclass are defined just like any other class, but they inherit from 'type'. The metaclass is automatically invoked when the class statement using metaclass ends. If a metaclass keyword is used on the other hand, the class assigned to it will be invoked instead of 'type',

>>> class MyMeta(type):
...     def __new__(cls, clsname, superclasses, attrdict):
...             print("class name:{}".format(clsname))
...             print("super class name{}".format(superclasses))
...             print("attribute dict{}".format(attrdict))
...             return type.__new__(cls, clsname, superclasses, attrdict)
...

Use the above class like below,

>>> class test1:
...     pass
...
>>> class test2(test1, metaclass=MyMeta):
...     pass
...

Output (Observe that we see that MyMeta__new__ is invoked and not from 'type')

class name:test2
super class name(<class '__main__.test1'>,)
attribute dict{'__module__': '__main__', '__qualname__': 'test2'}
>>>

Creating Singletons using Metaclass

The singleton pattern is a design pattern that puts a constraint in the instantiation of a class to only one object. Singleton design pattern is used in cases where exactly one object is required.

>>> class MySingleton(type):
...     _instances = {}
...     def __call__(cls, *args, **kwargs):
...             if cls not in cls._instances:
...                     cls._instances[cls] = super(MySingleton,cls).__call__(*args, **kwargs)
...             return cls._instances[cls]


>>> class SingletonClass(metaclass= MySingleton):
...     pass
...
>>> class RegularClass():
...     pass
...


>>> test1 = SingletonClass()
>>> test2 = SingletonClass()
>>>
>>> print(test1 == test2)
True
>>> test1 = RegularClass()
>>> test2 = RegularClass()
>>> print(test1 == test2)
False
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.