class A():
def hello(self):
print("Enter A")
print("Leave A")
class B(A):
def hello(self):
print("Enter B")
A.hello(self)
print("Leave B")
class C(A):
def hello(self):
print("Enter C")
A.hello(self)
print("Leave C")
class D(B, C):
def hello(self):
print("Enter D")
B.hello(self)
C.hello(self)
print("Leave D")
d = D()
d.hello() 运行的结果是:
Enter D
Enter B
Enter A
Leave A
Leave B
Enter C
Enter A
Leave A
Leave C
Leave D很显然,A中的hello()函数被执行了两次,是按照一个D,B,A, C,A这样一个顺序。这在有些情况下不是我们希望的代码执行顺序。更多的情况是,我们可能会要求按照某个顺序(MRO顺序,当前Python版本使用的是C3算法)不重复的调用祖先类中的方法。如:按照D,B,C,A这个顺序来调用hello()方法。 恰当的使用super,就能达到上述效果。
class A():
def hello(self):
print("Enter A")
print("Leave A")
class B(A):
def hello(self):
print("Enter B")
super(B, self).hello()
print("Leave B")
class C(A):
def hello(self):
print("Enter C")
super(C, self).hello()
print("Leave C")
class D(B, C):
def hello(self):
print("Enter D")
super(D, self).hello()
print("Leave D")
d = D()
d.hello() 运行结果:
Enter D
Enter B
Enter C
Enter A
Leave A
Leave C
Leave B
Leave D 涉及到多重继承的D类中的hello函数的B.hello(self) ,C.hello(self) 两条语句被super(D, self).hello() 代替,类B,C中的语句中也都使用了super的语句,这使得代码能够自动的跳转到对应的下一个函数中去执行,尤其是在类B中的super(B, self).hello() ,self的值为d,由d与B计算(用C3
算法来计算)出下一个要调用的函数是类C中的hello代码,完成了关键的“跳转”,而不是想当然的去调用A中的代码。然而,super这种“跳转”在处理多继承的时候,也会带来让人意想不到的烦恼,
就是调用函数,传值不匹配的问题,出现这种问题的根源还是super自身的机制,以上述的类B为例,在书写类B的代码时,使用super()时候我们肯定会按照他的父类中A中的方法的参数要求去传值,可是在代码运行时,B中的方法会去调用C中的方法,而不是A中的方法,这就可能会产生调用方法时的传值错误。
接下来再用一个例子来演示一下方法被重复调用可能带来的坏处:
把d看做数据,被类中的代码处理不同的次数,对d造成的影响可能是不同的。
class A():
def add(self):
self.data.append("Letter A")
self.count += 1