Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
5 views
in Python by (46k points)
Why we use metaclasses and what exactly is it?

3 Answers

+2 votes
by (10.9k points)
edited by

A metaclass is a class whose instances are classes,it defines the behavior of classes and its instances. Python allows us to use arbitrary callables for metaclasses but the better way is to create an actual class itself.In Python, a type is itself a class and it is its own type. To create your own metaclass all you need to do is subclass type.

A metaclass is also known as class-factory because when an object is created by calling a class,Python actually creates a new class by calling the metaclass. Metaclasses combined with methods like _init_ and _new_  gives you some extra allowance while creating a class,like-registering new class or replace the class with something.

Whenever Python meets a class statement,it executes its body. The resulting directory holds all the attributes of the class to be created and the metaclass is then determined and called based on the base classes of the new class which is to be created.

For better understanding refer to this example, the behavior of the class can also be changed by implementing methods like _add_,_iter_ and _getattr_ :

def make_hook(f):

    f.is_hook = 1

    return f 

class MyType(type):

    def __new__(mcls, name, bases, attrs): 

        if name.startswith('None'):

            return None

        newattrs = {}

        for attrname, attrvalue in attrs.iteritems():

            if getattr(attrvalue, 'is_hook', 0):

                newattrs['__%s__' % attrname] = attrvalue

            else:

                newattrs[attrname] = attrvalue 

        return super(MyType, mcls).__new__(mcls, name, bases, newattrs) 

    def __init__(self, name, bases, attrs):

        super(MyType, self).__init__(name, bases, attrs)

        print "Would register class %s now." % self 

    def __add__(self, other):

        class AutoClass(self, other):

            pass

        return AutoClass

    def unregister(self):

        print "Would unregister class %s now." % self 

class MyObject:

    __metaclass__ = MyType

class NoneSample(MyObject):

    pass

print type(NoneSample), repr(NoneSample) 

class Example(MyObject):

    def __init__(self, value):

        self.value = value

    @make_hook

    def add(self, other):

        return self.__class__(self.value + other.value)

Example.unregister() 

inst = Example(10)

print inst + inst

class Sibling(MyObject):

    pass

ExampleSibling = Example + Sibling

print ExampleSibling

print ExampleSibling.__mro__

0 votes
by (106k points)
edited by
  • In Python, a metaclass is the class of a class that defines how a class behaves and a class itself is an instance of a metaclass.

  • A class is also an object and just like other objects its instance is called Metaclass

  • type() is a special class that creates these objects. The type class is default metaclass which is responsible for making classes.

class abc:

    pass

print("Type of abc class is:", type(abc))

image

  • Metaclass creates Classes and Classes to create objects. This can summarize the whole metaclass.  

image

Learn in detail about Python by enrolling in Intellipaat Python Course online and upskill.

0 votes
by (20.3k points)
edited by

Let's see the easiest way of using the metaclass like this:

class MyMeta(type):

    counter = 0

   def __init__(cls, name, bases, dic):

        type.__init__(cls, name, bases, dic)

        cls._order = MyMeta.counter

        MyMeta.counter += 1

class MyType(object):              # Python 2

    __metaclass__ = MyMeta

class MyType(metaclass=MyMeta):    # Python 3

    pass

But, anything that's the subclass of MyType, gets a class attribute _order which records the order in which the classes are defined.

Related questions

0 votes
2 answers
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 4, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...