So, I only realised today that __new__
is deprecated for receiving arguments, as of python 2.6 (it isn\'t mentioned in the documentation, which is also not true
Well this made me curious because I did not see the deprecation in the documentation so I gave it a try myself.
class Foo(object):
def __new__(cls, a, b):
if a:
return a
elif b:
return b
else:
return super(Foo, cls).__new__(cls, a, b)
def __init__(self, a, b):
self.a = a
self.b = b
class Bar(Foo):
def __new__(cls, x, y):
if x:
return x
if y:
return y
else:
return super(Bar, cls).__new__(cls, x, y)
foo = Bar(False, False)
As you can see in this example I overrode the init in Foo because any args passed to new will be forwarded to the cls instance that __new__
attempts to create. The instance of foo with be of a Bar class but it will have members a and b. I caused the super class's __init__
to be called by not overriding it. The method __new__
always passes its args on to __init__
. If you don't override the __init__
for object it will fail since that method takes no args.
That's my take on the usage of new in Python 2.7. According to the docs 2.6 is similar.