After I had published my previous article, I got some feedback from my colleagues. And there was a simple (at first glance) but interesting question, that I am going to discuss. Why do I use __init__ method in my metaclass? Will __new__ one be more pythonic?
Indeed, all articles I have ever read describe metaclasses using __new__ method in their examples. Frankly, I used it too in the previous version of GreenRocket library. It was cargo cult. And I postponed publishing, before I had fixed that.
Nevertheless, the main goal of the previous article was to show, that we can use classes as regular objects. And it seems to be achieved. But metaclasses mechanism is not limited by this use case only. Python documentation says about it: “The potential uses for metaclasses are boundless. Some ideas that have been explored include logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization.” So you really need the power of __new__ method sometimes:
>>> class Meta(type): ... def __new__(meta, name, bases, attrs): ... filtered_bases =  ... for base in bases: ... if isinstance(base, type): ... filtered_bases.append(base) ... else: ... print(base) ... return type.__new__(meta, name, tuple(filtered_bases), attrs) ... >>> class Test(object, 'WTF!?', 'There are strings in bases!'): ... __metaclass__ = Meta ... WTF!? There are strings in bases! >>> Test.__mro__ (<class '__main__.Test'>, <type 'object'>)
However, I am pretty sure, that you have to avoid __new__ as much as you can. Because it significantly decreases flexibility. For example, what happens if you inherit a new class from another two with two different metaclasses?
>>> class AMeta(type): pass ... >>> class BMeta(type): pass ... >>> class A(object): __metaclass__ = AMeta ... >>> class B(object): __metaclass__ = BMeta ... >>> class C(A, B): pass ... Traceback (most recent call last): File "<stdin>", line 1, in <module> 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
As you can see, you get a conflict. You have to create a new metaclass based on both existing ones:
>>> class CMeta(AMeta, BMeta): pass ... >>> class C(A, B): __metaclass__ = CMeta ...
If these two metaclasses define just __init__ method, it will be simple:
>>> class CMeta(AMeta, Bmeta): ... def __init__(cls, name, bases, attrs): ... Ameta.__init__(cls, name, bases, attrs) ... Bmeta.__init__(cls, name, bases, attrs)
But if both of them define __new__ one, a walk in the park will turn to run through the hell. And this is not a hypothetical example. Try to mix in collections.Mapping to a model declaration class based on your favorite ORM. I got such task on my previous project.
In conclusion. Use __new__ method only if you are going to do something, which is unfeasible in __init__ one. And think twice, before copying code from examples. Even if the examples are from official documentation.