The Python Oracle

Any ideas about the best work around for __new__ losing its arguments?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Civilization

--

Chapters
00:00 Any Ideas About The Best Work Around For __new__ Losing Its Arguments?
01:28 Accepted Answer Score 13
02:18 Answer 2 Score 0
03:16 Answer 3 Score 5
03:33 Thank you

--

Full question
https://stackoverflow.com/questions/4028...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 13


__new__ is not "deprecated for receiving arguments". What changed in Python 2.6 is that object.__new__, the __new__ method of the object class, no longer ignores any arguments it's passed. (object.__init__ also doesn't ignore the arguments anymore, but that's just a warning in 2.6.) You can't use object as the terminating class for your inheritance if you want to pass arguments to __new__ or __init__.

In order for any code to rely on that behaviour to work in 2.6, you just have to replace object as the baseclass, using a baseclass that properly accepts the extra arguments and does not pass them along in the calls it makes (using super().)




ANSWER 2

Score 5


Thomas put me right in his answer, but I should add that the solution in my case was trivial: add a __new__ method to my base class with the lines:

class Base(object):
    def __new__(cls, *args, **kws):
        instance = super(Base, cls).__new__(cls)
        instance.__init__(*args, **kws)
        return instance



ANSWER 3

Score 0


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.