调用@classmethod的super,它调用另一个classmethod

时间:2019-06-26 09:51:59

标签: python-3.x super superclass class-method

在此处找不到答案,因此将其发布。 出现此错误:

main() takes 1 positional argument but 2 were given

对于此代码,B尝试扩展A的方法,而该方法将实习生调用其他类方法。

class A:
    @classmethod
    def run(cls):        
        cls.main(cls)

    @classmethod
    def main(cls):
        """Override and set main logic here
        """
        pass


class B(A):        
    title = "Hello"
    @classmethod
    def run(cls, name):
        cls.name = name
        super().run()

    @classmethod
    def main(cls, *args):
        """Override and set main logic here
        """
        print(cls.title, cls.name)

B.run('Bob')   # Hello Bob

1 个答案:

答案 0 :(得分:0)

super()通过:

(<class '__main__.B'>, <class '__main__.B'>)
B.main()调用时,

进入A.run(): 只需收集任何其他位置参数即可使B处理额外的参数:

def main(cls, *args):