使用super()而不是直接父级

upp*_*und 9 python super

这是super()的合法使用吗?

class A(object):
    def method(self, arg):
        pass

class B(A):
    def method(self, arg):
        super(B,self).method(arg)

class C(B):
    def method(self, arg):
        super(B,self).method(arg)
Run Code Online (Sandbox Code Playgroud)

谢谢.

Tho*_*s K 8

它会起作用,但它可能会让任何试图阅读你的代码的人感到困惑(包括你,除非你特别记得它).不要忘记,如果要从特定父类调用方法,您可以这样做:

A.method(self, arg)
Run Code Online (Sandbox Code Playgroud)