简体   繁体   English

调用@classmethod抽象超类错误— Python

[英]Calling @classmethod of abstract super class error — Python

I am trying to overwrite a classmethod as seen in the code below but am getting the following error 我正在尝试覆盖以下代码中所示的类方法,但出现以下错误

TypeError: must be type, not classobj

Why is this happening, and how can I fix it? 为什么会发生这种情况,我该如何解决?

   class A(object):
       @classmethod
       def test(cls, data):
         pass


    class B(A):
       @classmethod
       def test(cls, data):
         pass


    class C(B):
       @classmethod
       def test(cls, data):
         # do something to data
         return super(B, cls).test(data)

super is an object, not a class. super是一个对象,而不是一个类。 Since test is explicitly a class method, you need to call it using the class of super (in this case "A"). 由于test是明确的类方法,因此您需要使用super类(在这种情况下为“ A”)调用它。

Try replacing the last line with return A.test(data) 尝试用return A.test(data)替换最后一行

super only works with new-style classes (which in the 2.x series means inheriting from object ), but A is an old-style class. super仅适用于新样式类(在2.x系列中这意味着从object继承),但是A是旧样式类。

To fix it, replace class A(): with class A(object): . 要解决此问题,请将class A():替换为class A(object):

A is an old-style class (it does not inherit from object ), but super can only be used with new-style classes. A是旧式类(它不继承自object ),但是super 只能与新式类一起使用。 Just fix A : 只需解决A

class A(object):
    @classmethod
    def test(cls, data):
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM