Why does object.__new__ with arguments work fine in Python 2.x and not in Python 3.3+? -
why following code work fine in python 2.x , not in python 3.3+:
class testa(object): def __new__(cls, e): return super(testa, cls).__new__(testb, e) class testb(testa): def __init__(self, e): print(self, e) testa(1)
python 2.7.6 output:
(<__main__.testb object @ 0x7f6303378ad0>, 1)
python 3.1.5 output:
__main__:3: deprecationwarning: object.__new__() takes no parameters <__main__.testb object @ 0x7f2f69db8f10> 1
python 3.2.3 , 3.2.5 output:
<__main__.testb object @ 0xcda690> 1
python 3.3.5 , 3.4.1 output:
traceback (most recent call last): file "<stdin>", line 1, in <module> file "<stdin>", line 3, in __new__ typeerror: object() takes no parameters
object.__new__
has ignored arguments, , has issued deprecationwarning
@ least since python 2.6.
the reason why aren't seeing deprecationwarning
in 2.7 , 3.2 since 2.7 , 3.2 deprecationwarning
has been suppressed default; if use python -wd
or pythonwarnings=default
see warning.
in python 3.3 deprecationwarning
converted error.
the correct way write code (in version of python) swallow argument in testa.__new__
:
class testa(object): def __new__(cls, e): return super(testa, cls).__new__(testb)
since testb
derived testa
, argument will passed testb.__init__
.
Comments
Post a Comment