Python - 这是更好的方式调用超类'方法? [英] Python - which is the better way of calling superclass' method?

查看:240
本文介绍了Python - 这是更好的方式调用超类'方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用:

  SuperClass .__ init __(self,* args,** kwargs)

我的原因是显式地显示了使用哪个超类,特别是在多重继承的情况下。 >

但是,我遇到的其他代码使用

  super(MyClass,self) .__ init __(* args,** kwargs)



当它用于:

  class MyClass(SuperClass1,SuperClass2):
def __init __(self,* args,** kwargs):
super(MyClass,self).__ init __(* args,** kwargs)#which SuperClass正在使用?

我想知道为什么这种形式的电话被广泛采用?任何优点?

解决方案

super 现代(新样式)类是它允许协作多重继承。这里有一个例子。

 >>> class Foo(object):
... def display(self):
... printIn Foo
...
>>> class Foo2(Foo):
... def display(self):
... printIn Foo2
... super(Foo2,self).display $ b ... print返回Foo2
...
>>> class Bar(Foo):
... def display(self):
... printIn Bar
... super(Bar,self).display $ b ... print返回酒吧
...
>>> class FooBar(Foo2,Bar):
... pass
...
>>> FooBar()。display()
在Foo2
在Bar
在Foo
回到bar
回到Foo2
>> class BarFoo(Bar,Foo2):
... pass
...
>>> BarFoo()。display()
在Bar
在Foo2
在Foo
回到Foo2
回到Bar
请注意,我没有做任何事情改变超类上的显示方法,但我得到了通过更改我排列超类的顺序,在子类上创建不同的显示方法。 BarFoo FooBar 有不同的方法。这是因为他们有不同的方法解决方案订单

 >> BarFoo .__ mro__ 
(< class'__main __。BarFoo'>,< class'__main __。Bar'>,< class'__main __。Foo2'>,< class'__main __。Foo' ;,< type'object'>)
>>> FooBar .__ mro__
(< class'__main __。FooBar'>,< class'__main __。Foo2'>,< class'__main __。Bar'>,< class'__main __。Foo' ;,< type'object'>)

这意味着 super 为它调用的每个子类解析为不同的类。这允许每个重写方法改变一小部分的事情,并且仍然让每个其他超类都贡献给方法调用, 愿意玩得很好。


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__
(<class '__main__.BarFoo'>, <class '__main__.Bar'>, <class '__main__.Foo2'>, <class '__main__.Foo'>, <type 'object'>)
>>> FooBar.__mro__
(<class '__main__.FooBar'>, <class '__main__.Foo2'>, <class '__main__.Bar'>, <class '__main__.Foo'>, <type 'object'>)

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.

这篇关于Python - 这是更好的方式调用超类'方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆