When (and why) was Python `__new__()` introduced?
The blog post The Inside Story on New-Style Classes
(from the aptly named http://python-history.blogspot.com
) written by Guido van Rossum
(Python's BDFL) provides some good information regarding this subject.
Some relevant quotes:
New-style classes introduced a new class method
__new__()
that lets the class author customize how new class instances are created. By overriding__new__()
a class author can implement patterns like the Singleton Pattern, return a previously created instance (e.g., from a free list), or to return an instance of a different class (e.g., a subclass). However, the use of__new__
has other important applications. For example, in the pickle module,__new__
is used to create instances when unserializing objects. In this case, instances are created, but the__init__
method is not invoked.Another use of
__new__
is to help with the subclassing of immutable types. By the nature of their immutability, these kinds of objects can not be initialized through a standard__init__()
method. Instead, any kind of special initialization must be performed as the object is created; for instance, if the class wanted to modify the value being stored in the immutable object, the__new__
method can do this by passing the modified value to the base class__new__
method.
You can read the entire post for more information on this subject.
Another post about New-style Classes
which was written along with the above quoted post has some additional information.
Edit:
In response to OP's edit and the quote from the Zen of Python, I would say this.
Zen of Python was not written by the creator of the language but by Tim Peters and was published only in August 19, 2004. We have to take into account the fact that __new__
appears only in the documentation of Python 2.4 (which was released on November 30, 2004), and this particular guideline (or aphorism) did not even exist publicly when __new__
was introduced into the language.
Even if such a document of guidelines existed informally before, I do not think that the author(s) intended them to be misinterpreted as a design document for an entire language and ecosystem.
I will not explain the history of __new__
here because I have only used Python since 2005, so after it was introduced into the language. But here is the rationale behind it.
The normal configuration method for a new object is the __init__
method of its class. The object has already been created (usually via an indirect call to object.__new__
) and the method just initializes it. Simply, if you have a truely non mutable object, it is too late.
In that use case the Pythonic way is the __new__
method, which builds and returns the new object. The nice point with it, is that is is still included in the class definition and does not require a specific metaclass. Standard documentation states:
new() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.
Defining a __call__
method on the metaclass is indeed allowed but is IMHO non Pythonic, because __new__
should be enough. In addition, __init__
, __new__
and metaclasses each dig deeper inside the internal Python machinery. So the rule shoud be do not use __new__
if __init__
is enough, and do not use metaclasses if __new__
is enough.