从Python中的子类调用父类的方法?


从Python中的子类调用父类的方法?


使用super()

class Foo(Bar):
    def baz(self, arg):
        return super(Foo, self).baz(arg)

Python也有超级:

super(type[, object-or-type])

例:

class A(object):     # deriving from 'object' declares A as a 'new-style-class'
    def foo(self):
        print "foo"

class B(A):
    def foo(self):
        super(B, self).foo()   # calls 'A.foo()'

myB = B()
myB.foo()