在Python中创建简单的对象层次结构时,我希望能够从派生类调用父类的方法。在Perl和Java中,对此有一个关键字(super)。在Perl中,我可以这样做:

package Foo;

sub frotz {
    return "Bamf";
}

package Bar;
@ISA = qw(Foo);

sub frotz {
   my $str = SUPER::frotz();
   return uc($str);
}

在Python中,似乎我必须显式地从子类中命名父类。 在上面的例子中,我必须做一些类似于Foo::frotz()的事情。

这似乎是不正确的,因为这种行为使得很难建立深层的等级制度。如果孩子们需要知道哪个类定义了一个继承的方法,那么就会产生各种各样的信息麻烦。

这是python的实际限制,还是我理解上的差距,或者两者兼而有之?


当前回答

如果你不知道你可能会得到多少争论,并且想把它们都传递给孩子:

class Foo(bar)
    def baz(self, arg, *args, **kwargs):
        # ... Do your thing
        return super(Foo, self).baz(arg, *args, **kwargs)

(来自:Python -覆盖__init__的最干净的方法,其中可选的kwarg必须在super()调用后使用?)

其他回答

Python中也有一个super()。它有点不稳定,因为Python有新旧风格的类,但它非常常用,例如在构造函数中:

class Foo(Bar):
    def __init__(self):
        super(Foo, self).__init__()
        self.baz = 5

在这个例子中,cafec_param是一个基类(父类),abc是一个子类。abc调用基类中的AWC方法。

class cafec_param:

    def __init__(self,precip,pe,awc,nmonths):

        self.precip = precip
        self.pe = pe
        self.awc = awc
        self.nmonths = nmonths

    def AWC(self):

        if self.awc<254:
            Ss = self.awc
            Su = 0
            self.Ss=Ss
        else:
            Ss = 254; Su = self.awc-254
            self.Ss=Ss + Su   
        AWC = Ss + Su
        return self.Ss


    def test(self):
        return self.Ss
        #return self.Ss*4

class abc(cafec_param):
    def rr(self):
        return self.AWC()


ee=cafec_param('re',34,56,2)
dd=abc('re',34,56,2)
print(dd.rr())
print(ee.AWC())
print(ee.test())

输出

56

56

56

下面是一个使用super()的例子:

#New-style classes inherit from object, or from another new-style class
class Dog(object):

    name = ''
    moves = []

    def __init__(self, name):
        self.name = name

    def moves_setup(self):
        self.moves.append('walk')
        self.moves.append('run')

    def get_moves(self):
        return self.moves

class Superdog(Dog):

    #Let's try to append new fly ability to our Superdog
    def moves_setup(self):
        #Set default moves by calling method of parent class
        super(Superdog, self).moves_setup()
        self.moves.append('fly')

dog = Superdog('Freddy')
print dog.name # Freddy
dog.moves_setup()
print dog.get_moves() # ['walk', 'run', 'fly']. 
#As you can see our Superdog has all moves defined in the base Dog class

Python也有super:

超级(类型、对象或者类型)

返回一个代理对象,该对象将方法调用委托给类型的父类或兄弟类。 这对于访问在类中被重写的继承方法非常有用。 除了类型本身被跳过之外,搜索顺序与getattr()所使用的相同。

例子:

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()

如果您想调用任何类的方法,您可以简单地调用class。方法在类的任何实例上执行。如果你的继承相对干净,这也适用于子类的实例:

class Foo:
    def __init__(self, var):
        self.var = var
    
    def baz(self):
        return self.var

class Bar(Foo):
    pass

bar = Bar(1)
assert Foo.baz(bar) == 1