python 超类,Python - 这是更好的方式调用超类'方法?

All the while I have been using:

SuperClass.__init__(self, *args, **kwargs)

My reason is that this shows explicitly which superclass is used, especially in the case of multiple inheritance.

However, other codes I came across use

super(MyClass, self).__init__(*args, **kwargs)

instead.

This could become ambigious when it's used in:

class MyClass(SuperClass1, SuperClass2):

def __init__(self, *args, **kwargs):

super(MyClass, self).__init__(*args, **kwargs) #which SuperClass is being used?

I would like to know why this form of calling is widely adopted? Any advantage at all?

解决方案

The reason that super is prefereable for modern (new style) classes is that it allows cooperative multiple inheritance. Here's an example.

>>> class Foo(object):

... def display(self):

... print "In Foo"

...

>>> class Foo2(Foo):

... def display(self):

... print "In Foo2"

... super(Foo2, self).display()

... print "Back in Foo2"

...

>>> class Bar(Foo):

... def display(self):

... print "In Bar"

... super(Bar, self).display()

... print "Back in Bar"

...

>>> class FooBar(Foo2, Bar):

... pass

...

>>> FooBar().display()

In Foo2

In Bar

In Foo

Back in Bar

Back in Foo2

>>> class BarFoo(Bar, Foo2):

... pass

...

>>> BarFoo().display()

In Bar

In Foo2

In Foo

Back in Foo2

Back in Bar

Note that I didn't do anything to change the display method on the superclasses but I got different display methods on the subclasses by changing the order in which I arranged the superclasses. BarFoo and FooBar have different methods. This is because they have different Method Resolution Orders

>>> BarFoo.__mro__

(, , , , )

>>> FooBar.__mro__

(, , , , )

This means that super resolves to a different class for each subclass that it's called in. This allows for each overriding method to change a small part of what's going on and still let every other superclass contribute to the method call as long as they're willing to play nicely.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值