我如何从Python的子类中称呼间接的父级方法?
我有A,B和C类。C从A和B继承,并且在大多数情况下,有方法互相覆盖,我希望A覆盖B,所以我有以下结构:
class A:
def some_method(self):
return self.some_attribute
class B:
def some_method(self):
return self.some_other_attribute
class C(A,B):
def some_method(self):
var = super().some_method() -> this will give me the method from A, but I want the B one
return var + 1
所以我的问题是如何如何我使用super()或其他方式调用some_method()
,因此我可以在c中的方法中使用它?
同样为记录,我正在使用Python 3.**
I have classes A, B, and C. C inherits both from A and B, and for the most part wherever there are methods overriding each other I want A's to override B's, so I have the following structure:
class A:
def some_method(self):
return self.some_attribute
class B:
def some_method(self):
return self.some_other_attribute
class C(A,B):
def some_method(self):
var = super().some_method() -> this will give me the method from A, but I want the B one
return var + 1
So my question is how can I invoke with super() or some other way the some_method()
from B, so I can use it in the method override in C?
Also for the record I am using Python 3.*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Python是一种具有多种继承的语言,这意味着类可以具有几个直接超类。但是,为了调用
super()
,python始终线性化的继承层次结构以确定谁是“最佳”超级类别。这就是称为方法解决顺序,通过<代码> __ Mro __ python中的变量。因此,如果我们具有类层次结构
,则虽然
d
有两个超级类,如果我们在d
中说super()
,它只会给我们b
。这是因为d
的MRO为我们创建了线性层次结构。在Python 3中,它使用了一种称为 C3分辨率的算法我们在类
d
上的方法中调用super()
,我们将在b
上获取版本,如果我们调用super super ()
从那里,我们将获得c
版本,然后获得a
版本,最后是Object
一个。因此,就您而言,
c
从a
和b
(按此顺序)继承,因此它将偏向于a < /代码>。也就是说,示例中类
a ,则只需切换超级类的顺序即可。c
的MRO是(c,a,b,object)
。如果您希望它能始终b
然后这是最好的解决方案。它的可预测性,Python程序员将立即了解此类中
super()
调用的意图。如果适合您的需求,我建议这样做。但是,如果您有一个复杂的多启示系统,并且需要访问
a
和b
超级类直接使用类名称致电他们,放弃super()
完全致电。如果
some_method
进行一些参数,则可以在显式self
参数之后通过它们。这将引起更多的眉毛,因此,如果您发现自己需要以复杂的方式访问两种不同的超类方法,则可以考虑重构代码以使用较少的多重继承。但是,如果您确实需要它,则该功能就在那里。Python is a language with multiple inheritance, which means that a class can have several direct superclasses. But for the purposes of calling
super()
, Python always linearizes the inheritance hierarchy to determine who the "best" superclass is. This is called the method resolution order, accessed via the__mro__
variable in Python.So if we have the class hierarchy
Then although
D
has two superclassses, if we saysuper()
inD
, it'll only give usB
. That's because the MRO forD
creates a linear hierarchy for us. In Python 3, it uses an algorithm called the C3 resolution, which gives us this hierarchySo if we call
super()
in a method on classD
, we'll get the version onB
, and if we callsuper()
from there, we'll get theC
version, then theA
version, and finally theobject
one.So in your case,
C
inherits fromA
andB
(in that order), so it's going to be biased towardA
. That is, the MRO for the classC
in your example is(C, A, B, object)
. If you want it to always go forB
and thenA
, then simply switch the order of the superclasses.This is the best solution. It's predictable and Python programmers will immediately understand the intention of a
super()
call in this class. I recommend this if it fits your needs.However, if you have a complicated multi-inheritance system and need access to both the
A
andB
superclasses at the same time, you can always call them directly with a class name, forgoing thesuper()
call altogether.If
some_method
takes some arguments, you can pass them after the explicitself
argument. This is going to raise more eyebrows, so if you find yourself needing access to two distinct superclass methods in complicated ways, you might consider refactoring your code to use less multiple inheritance. But the feature is there if you really need it.