Perché il seguente codice funzionano bene in Python 2.x e non in Python 3.3+:Perché object .__ new__ con argomenti funziona bene in Python 2.xe non 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 uscita:
(<__main__.TestB object at 0x7f6303378ad0>, 1)
Python 3.1.5 uscita:
__main__:3: DeprecationWarning: object.__new__() takes no parameters
<__main__.TestB object at 0x7f2f69db8f10> 1
Python 3.2.3 e 3.2.5 di uscita:
<__main__.TestB object at 0xcda690> 1
Python 3.3.5 e 3.4.1 di uscita:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __new__
TypeError: object() takes no parameters
Correlato: [Perché oggetto. \ _ \ _ Nuovo \ _ \ _ funziona in modo diverso in questi tre casi] (http://stackoverflow.com/q/19277399) –
È l'output dell'interprete, prima stampa, secondo oggetto. Se eseguito come programma sarà una linea. Fisso. – tbicr
Sei sicuro? Questo codice funziona su Python 2.7.4 e Python 3.2.3. –