如何在Python 3.0中调用super()?

Cas*_*ash 0 python python-3.x

我有一些奇怪的错误,我在Python(版本3.0)中看到了一段时间.

更改函数的签名会影响是否super()有效,尽管它不带参数.你能解释一下为什么会这样吗?

谢谢,

克里斯

>>> class tmp:
...     def __new__(*args):
...             super()
... 
>>> tmp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __new__
SystemError: super(): no arguments
>>> class tmp:
...     def __new__(mcl,*args):
...             super()
... 
>>> tmp()
>>>
Run Code Online (Sandbox Code Playgroud)

Ale*_*lli 6

正如文档所说,"零参数形式自动在堆栈帧中搜索class(__class__)和第一个参数." 你的第一个例子__new__没有第一个参数 - 它声称它可以用零个或多个参数调用,所以无争议super就是难倒.您的第二个示例具有明确的第一个参数,因此堆栈帧中的搜索成功.