在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的实际限制,还是我理解上的差距,或者两者兼而有之?


当前回答

在Python 2中,我使用super()时运气不太好。我用的答案是 jimifiki在这个SO线程上如何引用python中的父方法? 然后,我添加了我自己的小改动,我认为这在可用性方面是一个改进(特别是如果您有很长的类名)。

在一个模块中定义基类:

 # myA.py

class A():     
    def foo( self ):
        print "foo"

然后导入类到另一个模块作为父模块:

# myB.py

from myA import A as parent

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

其他回答

class a(object):
    def my_hello(self):
        print "hello ravi"

class b(a):
    def my_hello(self):
    super(b,self).my_hello()
    print "hi"

obj = b()
obj.my_hello()

使用super()函数:

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

对于Python < 3,必须显式选择使用new-style类并使用:

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

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

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

class Foo(Bar):
    def __init__(self):
        super(Foo, self).__init__()
        self.baz = 5
class department:
    campus_name="attock"
    def printer(self):
        print(self.campus_name)

class CS_dept(department):
    def overr_CS(self):
        department.printer(self)
        print("i am child class1")

c=CS_dept()
c.overr_CS()