python3 super 多继承,python3 - super()对多继承的行为

I know that super() and multi-inheritance have already been discussed here. But I did not find a solution, regarding my specific problem in python3.

Let's assume we have:

#! /usr/bin/env python3

class A(object):

def __init__(self):

super().__init__()

def foo(self):

print("The")

class B(object):

def __init__(self):

super().__init__()

def foo(self):

print("world")

class C(B):

def __init__(self):

super().__init__()

def foo(self):

super().foo()

print("is")

class D(A,C):

def __init__(self):

super().__init__()

def foo(self):

super().foo()

print("nice")

d = D()

d.foo()

This will get me:

The

nice

On the other hand, if I change the order of inheritance in D() to:

class D(C,A):

def __init__(self):

super().__init__()

def foo(self):

super().foo()

print("nice")

It gives me

world

is

nice

I, however, only get the desired output:

The

world

is

nice

using:

class D(C,A):

def __init__(self):

super().__init__()

def foo(self):

A.foo(self)

C.foo(self)

print("nice")

Which I find quite unelegant.

So my question is: Is it possible to use super() in python3 invoking the super method of both super classes instead of just the first?

解决方案

Unfortunately, without a knowledge of the method resolution order (MRO) of D, there is no way to call super() in D to get at both base classes.

But the MRO is powerful concept. In the second case,

class D(C,A):

...

the (MRO) is

>>> D.mro()

[, , , , ]

Inasmuch as calling super() takes you to the next class in the MRO, as Mr. Pieters stated, and you want the print statements coming in the order of A, B, C, then D, simply put super().foo() first and print(...) second in each definition of foo(). The only exception is do not put super().foo() in class A because foo() is not defined in object.

Solution

#! /usr/bin/env python3

class A(object):

def __init__(self):

super().__init__()

def foo(self):

print("The")

class B(object):

def __init__(self):

super().__init__()

def foo(self):

super().foo() # Inserted

print("world")

class C(B):

def __init__(self):

super().__init__()

def foo(self):

super().foo()

print("is")

class D(C,A): # Correct ordering

def __init__(self):

super().__init__()

def foo(self):

super().foo()

print("nice")

d = D()

d.foo()

Alternate solution

The MRO of D(A,C) in the first case includes all classes as well, so with correct ordering of super() and print(...) statements, one can make it work:

class A(object):

def foo(self):

print("The")

super().foo()

class B(object):

def foo(self):

print("world")

class C(B):

def foo(self):

super().foo()

print("is")

class D(A,C):

def foo(self):

super().foo()

print("nice")

D().foo()

Further reading

To understand inheritance order (e.g. class D(C,A) or class D(A,C)) and MRO, see https://www.python.org/download/releases/2.3/mro/. The C3 method resolution order is described in detail and there are nicely drawn ASCII class hierarchies with the MRO labeled.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值