Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customary To Inherit Metaclasses From type?

I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass like

def metacls(clsName, bases, atts):     ....     return type(clsName, bases, atts) 

However, I have seen a lot of people write their metaclasses in the following way:

class Metacls(type):     def __new__(meta, clsName, bases, atts):         ....         return type.__new__(meta, clsName, bases, atts) 

As far as I can see, these would both do the same thing. Is there any reason to use the base class instead? Is it customary?

like image 533
Nikwin Avatar asked Jan 27 '10 19:01

Nikwin


People also ask

Are metaclasses inherited?

Every object and class in Python is either an instance of a class or an instance of a metaclass. Every class inherits from the built-in basic base class object , and every class is an instance of the metaclass type .

Why would you use metaclasses?

A metaclass is most commonly used as a class-factory. When you create an object by calling the class, Python creates a new class (when it executes the 'class' statement) by calling the metaclass.

What is class type'> in Python?

A class is a Python data structure that can be used as a template for instances of that class by calling it, e.g. foo = Foo() . A type is a class that can be used as a template for additional classes by way of inheritance, e.g. class Foo(Bar):

Why metaclasses in Python?

A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass. A class in Python defines how the instance of the class will behave. In order to understand metaclasses well, one needs to have prior experience working with Python classes.


1 Answers

There are subtle differences, mostly relating to inheritance. When using a function as a metaclass, the resulting class is really an instance of type, and can be inherited from without restriction; however, the metaclass function will never be called for such subclasses. When using a subclass of type as a metaclass, the resulting class will be an instance of that metaclass, as will any of its subclasses; however, multiple inheritance will be restricted.

Illustrating the differences:

>>> def m1(name, bases, atts): >>>     print "m1 called for " + name >>>     return type(name, bases, atts) >>>  >>> def m2(name, bases, atts): >>>     print "m2 called for " + name >>>     return type(name, bases, atts) >>>  >>> class c1(object): >>>     __metaclass__ = m1 m1 called for c1  >>> type(c1) <type 'type'>  >>> class sub1(c1): >>>     pass  >>> type(sub1) <type 'type'>  >>> class c2(object): >>>     __metaclass__ = m2 m2 called for c2  >>> class sub2(c1, c2): >>>     pass  >>> type(sub2) <type 'type'> 

Note that when defining sub1 and sub2, no metaclass functions were called. They will be created exactly as if c1 and c2 had no metaclasses, but instead had been manipulated after creation.

>>> class M1(type): >>>     def __new__(meta, name, bases, atts): >>>         print "M1 called for " + name >>>         return super(M1, meta).__new__(meta, name, bases, atts)  >>> class C1(object): >>>     __metaclass__ = M1 M1 called for C1  >>> type(C1) <class '__main__.M1'>  >>> class Sub1(C1): >>>     pass M1 called for Sub1  >>> type(Sub1) <class '__main__.M1'> 

Note the differences already: M1 was called when creating Sub1, and both classes are instances of M1. I'm using super() for the actual creation here, for reasons which will become clear later.

>>> class M2(type): >>>     def __new__(meta, name, bases, atts): >>>         print "M2 called for " + name >>>         return super(M2, meta).__new__(meta, name, bases, atts)  >>> class C2(object): >>>     __metaclass__ = M2 M2 called for C2  >>> type(C2) <class '__main__.M2'>  >>> class Sub2(C1, C2): >>>     pass M1 called for Sub2 Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "<stdin>", line 23, in __new__ TypeError: Error when calling the metaclass bases     metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases 

This is the major restriction on multiple inheritance with metaclasses. Python doesn't know whether M1 and M2 are compatible metaclasses, so it forces you to create a new one to guarantee that it does what you need.

>>> class M3(M1, M2): >>>     def __new__(meta, name, bases, atts): >>>         print "M3 called for " + name >>>         return super(M3, meta).__new__(meta, name, bases, atts)  >>> class C3(C1, C2): >>>     __metaclass__ = M3 M3 called for C3 M1 called for C3 M2 called for C3  >>> type(C3) <class '__main__.M3'> 

This is why I used super() in the metaclass __new__ functions: so each one can call the next one in the MRO.

Certain use cases might need your classes to be of type type, or might want to avoid the inheritance issues, in which case a metaclass function is probably the way to go. In other cases, the type of the class might be truly important, or you might want to operate on all subclasses, in which case subclassing type would be a better idea. Feel free to use the style that fits best in any given situation.

like image 88
eswald Avatar answered Oct 24 '22 01:10

eswald