【问题标题】:How to call super() in Python 3.0?如何在 Python 3.0 中调用 super()?
【发布时间】:2010-11-24 06:20:56
【问题描述】:

我遇到了一段时间以来在 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()
>>>

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    正如the docs 所说,“零参数形式会自动在堆栈框架中搜索类 (__class__) 和第一个参数。” __new__ 的第一个示例没有第一个参数 - 它声称可以使用零个或多个参数调用它,因此无参数 super 被难住了。您的第二个示例确实有一个明确的第一个参数,因此在堆栈帧中的搜索成功。

    【讨论】:

      【解决方案2】:

      python 3.0 new super 试图在这里动态地为你做出选择,阅读这个 PEP here 应该可以解释一切。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 2018-05-27
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        相关资源
        最近更新 更多