The Changelog for Python 2.2 (where new-style classes were introduced) says the following about the __new__
function:
__new__
is a static method, not a class method. I initially thought it would have to be a class method, and that's why I added theclassmethod
primitive. Unfortunately, with class methods, upcalls don't work right in this case, so I had to make it a static method with an explicit class as its first argument.
However, I cannot think of why class methods wouldn't work for this purpose, and it would certainly look better. Why didn't __new__
end up as a class method in the end? What does Guido refer to when he says that "upcalls don't work right in this case"?
__new__
being static method allows a use-case when you create an instance of a subclass in it:If
new
is a class method then the above is written as:and there is no place to put
subcls
.A more common case would be to call parent
__new__
without usingsuper()
. You need a place to passcls
explicitly in this case: