In Python 2:
>>> class A:
... pass
...
>>> A.__new__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute '__new__'
>>> class A(object):
... pass
...
>>> A.__new__
<built-in method __new__ of type object at 0x1062fe2a0>
Conclusion: object
contains __new__
and subclasses inherit the method.
in Python 3:
>>> class A:
... pass
...
>>> A.__new__
<built-in method __new__ of type object at 0x100229940>
__new__
is a defined method in our class, without any inheritance. How does this work? Where does __new__
"come from"?