Python super() 引发 TypeError

发布于 2024-07-12 11:49:11 字数 531 浏览 7 评论 0原文

在 Python 2.5 中,以下代码会引发 TypeError

>>> class X:
      def a(self):
        print "a"

>>> class Y(X):
      def a(self):
        super(Y,self).a()
        print "b"

>>> c = Y()
>>> c.a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in a
TypeError: super() argument 1 must be type, not classobj

如果我将 class X 替换为 class X(object),它将起作用。 对此有何解释?

In Python 2.5, the following code raises a TypeError:

>>> class X:
      def a(self):
        print "a"

>>> class Y(X):
      def a(self):
        super(Y,self).a()
        print "b"

>>> c = Y()
>>> c.a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in a
TypeError: super() argument 1 must be type, not classobj

If I replace the class X with class X(object), it will work. What's the explanation for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぇ气 2024-07-19 11:49:11

原因是 super() 仅在 新样式类 上运行,其中 2 .x 系列表示从 object 扩展:

>>> class X(object):
        def a(self):
            print 'a'

>>> class Y(X):
        def a(self):
            super(Y, self).a()
            print 'b'

>>> c = Y()
>>> c.a()
a
b

The reason is that super() only operates on new-style classes, which in the 2.x series means extending from object:

>>> class X(object):
        def a(self):
            print 'a'

>>> class Y(X):
        def a(self):
            super(Y, self).a()
            print 'b'

>>> c = Y()
>>> c.a()
a
b
〃温暖了心ぐ 2024-07-19 11:49:11

另外,除非必要,否则不要使用 super()。 您可能会怀疑,这并不是与新式类相关的通用“正确的事情”。

有时您期望多重继承,并且可能需要它,但在您了解 MRO 的详细细节之前,最好不要管它并坚持:

 X.a(self)

In addition, don't use super() unless you have to. It's not the general-purpose "right thing" to do with new-style classes that you might suspect.

There are times when you're expecting multiple inheritance and you might possibly want it, but until you know the hairy details of the MRO, best leave it alone and stick to:

 X.a(self)
独留℉清风醉 2024-07-19 11:49:11

如果上面的答案都没有明确提到这一点。 您的父类需要继承“object”,这实际上会将其变成一个新的样式类。

# python 3.x:
class ClassName(object): # This is a new style class
    pass

class ClassName: # This is also a new style class ( implicit inheritance from object )
    pass

# Python 2.x:
class ClassName(object): # This is a new style class
    pass

class ClassName:         # This is a old style class
    pass

In case none of the above answers mentioned it clearly. Your parent class needs to inherit from "object", which would essentially turn it into a new style class.

# python 3.x:
class ClassName(object): # This is a new style class
    pass

class ClassName: # This is also a new style class ( implicit inheritance from object )
    pass

# Python 2.x:
class ClassName(object): # This is a new style class
    pass

class ClassName:         # This is a old style class
    pass
月竹挽风 2024-07-19 11:49:11

我尝试了各种 Xa() 方法; 但是,它们似乎需要 X 的实例才能执行 a(),因此我执行了 X().a(self),这似乎比以前的答案更完整,至少对于我遇到的应用程序来说是这样。 这似乎不是解决问题的好方法,因为存在不必要的构建和破坏,但效果很好。

我的具体应用程序是 Python 的 cmd.Cmd 模块,由于某种原因,它显然不是 NewStyle 对象。

最后结果:

X().a(self)

I tried the various X.a() methods; however, they seem to require an instance of X in order to perform a(), so I did X().a(self), which seems more complete than the previous answers, at least for the applications I've encountered. It doesn't seem to be a good way of handling the problem as there is unnecessary construction and destruction, but it works fine.

My specific application was Python's cmd.Cmd module, which is evidently not a NewStyle object for some reason.

Final Result:

X().a(self)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文