Reputation: 12687
What is the correct way to implement the standard behaviour of __new__
in Python so that no functionality is broken?
I used
class Test:
def __new__(cls, *args, **kwargs):
return object.__new__(cls, *args, **kwargs)
t=Test()
which on some Python versions throws DepreciationWarnings. On the internet I had seen something with super()
or with type()
. What are the differences and which is prefered?
Upvotes: 6
Views: 1905
Reputation: 157344
You should write
return super(Test, cls).__new__(cls, *args, **kwargs)
This is recommended by the documentation (and the same for 2.x).
The reason to use super
is as always to cope with inheritance tree linearisation; you don't know for certain that the appropriate superclass is object
, so you should always use super
.
Upvotes: 7