Python TypeError:object.\uuuu init\uuuu()不接受任何参数
我被Python中的这种继承行为难住了。据我所知,超级类构造函数的调用是正确的,尽管语法从2.7到3.5有所不同Python TypeError:object.\uuuu init\uuuu()不接受任何参数,python,python-3.x,inheritance,Python,Python 3.x,Inheritance,我被Python中的这种继承行为难住了。据我所知,超级类构造函数的调用是正确的,尽管语法从2.7到3.5有所不同 Python 2.7.11 |Continuum Analytics, Inc.| (default, Dec 6 2015, 18:08:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2 Type "help", "copyright", "credits" or "license" for more informatio
Python 2.7.11 |Continuum Analytics, Inc.| (default, Dec 6 2015, 18:08:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>>
>>> import pandas as pd
>>> class MyTimestamp(pd.Timestamp):
... def __init__(self, arg, **kwargs):
... super(MyTimestamp, self).__init__(arg, **kwargs)
...
>>> a=MyTimestamp(1312342152423, unit='us')
>>> a
Timestamp('1970-01-16 04:32:22.152423')
使用Python3.5.1解释器,我得到了以下结果,它们都安装了相同版本的Pandas(0.18.0),但我相信这更像是Python继承的东西
Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7 2015, 11:16:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pandas as pd
>>> class MyTimestamp(pd.Timestamp):
... def __init__(self, arg, **kwargs):
... super().__init__(arg, **kwargs)
...
>>> a=MyTimestamp(1312342152423, unit='us')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
TypeError: object.__init__() takes no parameters
因此,
object
不是分辨率顺序中的下一个类。这个错误是从哪里来的?也许更重要的是,我做了哪些不正确的事情,我可以通过更改来解决问题?我认为这与您使用super没有任何关系。如果您用另一个类测试相同的代码,它工作得很好。例如:
class Foo:
def __init__(self, arg):
print ("Foo",arg)
class Bar(Foo):
def __init__(self, arg):
super().__init__(arg)
b=Bar(1) #Prints "Foo 1"
Pandas似乎对Timestamp类做了一些奇怪的事情,我不知道具体是什么,但这与C扩展有关:
无论如何,以下各项似乎都能如期发挥作用:
import pandas
class MyTimestamp(pandas.Timestamp):
def __init__(self, arg, **kwargs):
pass
a=MyTimestamp(1312342152423, unit='us') #Timestamp('1970-01-16 04:32:22.152423')
另外,请看一下这两种定义之间的输出差异:
import pandas
class MyTimestamp(pandas.Timestamp):
def __init__(self, arg, **kwargs):
print(super().__init__)
class Foo:
def __init__(self, arg):
print ("Foo",arg)
class Bar(Foo):
def __init__(self, arg):
print(super().__init__)
a=MyTimestamp(1612342152423, unit='us')
b=Bar(1)
这将产生:
<method-wrapper '__init__' of MyTimestamp object at 0x04981AD0>
<bound method Foo.__init__ of <__main__.Bar object at 0x033C2070>>
问题是,在这种情况下,您应该覆盖
\uuuuu new\uuuu
。panda的时间戳似乎是不可变的(就像它从中继承的datetime一样),所以它的\uuuu init\uuuu
方法什么都不做
您编写它的方式在Python3中运行良好。用您自己的自定义类替换pd.Timestamp
,您将看到super被正常调用
类似的问题和解决方案:Hmm。。。在我看来,你的超人不一样。python2.x版本不应该是
super(MyTimestamp,self)。\uuu init\uuu(arg,**kwargs)
?(也就是说,在python2.x版本中,你有一个额外的self
)@mgilson:True(修复了这个问题),但它仍然得到相同的结果注意Python3仍然支持python2语法。是的,看起来应该这样做:\u new\u
:谢谢,这非常有用。你是对的:decimal.decimal
也会发生同样的情况,这是一个C扩展。
<method-wrapper '__init__' of MyTimestamp object at 0x04981AD0>
<bound method Foo.__init__ of <__main__.Bar object at 0x033C2070>>