Python中super()的使用(一),, 1.super被引


1.super被引入的初衷

super()通常是被说成super函数,其实它是一个内置的类,是在Python2.2中新增加的,super()实例化一个super对象,这个super对象充当一个访问代理的角色,它帮助子类的对象访问父类,祖父类以及所有祖先类中被方法(尤其是访问那些被子类重写的方法)。

在super类没有出现以前,如果要在子类方法中去调用父类的方法,必须显式的使用父类的类名,用一种非绑定的方式去调用。如下例子(所有例子程序均在Python3.4下实现)所示:

class A(): def __init__(self): print("enter A") print("leave A")

class B(A): def __init__(self): print("enter B") A.__init__(self) print("leave B") >>> b = B() enter B enter A leave A leave B

虽然使用A.__init__(self) 来调用父类的方法,浅显易懂,但是,一个潜在的问题就是,如果B的父类变成其他类比如C,相应的就需要修改原代码为C.__init__(self) ,代码量如果巨大的话,这将是一项繁琐的工作。如果能有一条语句,它不需要显式的使用父类的名字,它只需要传入当前类名,当前类的实例,就能自动计算出父类,这样就可以灵活的应对类结构的变化。super(B,self).__init__()语句可以恰当的替代A.__init__(self) 语句。

Python还支持多重继承,在多重继承中使用A.__init__(self) 这种方式来调用祖先类的方法,还会产生祖先类的方法会被多次重复调用的情况,这也会产生潜在的问题。

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 DEnter BEnter ALeave ALeave BEnter CEnter ALeave ALeave CLeave 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 DEnter BEnter CEnter ALeave ALeave CLeave BLeave 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 += 1class B(A):    def add(self):        self.data.append("Letter B")        self.count += 1        super(B, self).add()class C(A):    def add(self):        self.data.append("Letter C")        self.count += 1        super(C, self).add()class D(B, C):    def __init__(self):        self.count = 0        self.data = []    def add(self):        self.data.append("Letter D")        self.count += 1        super(D, self).add()d = D()d.add()print(d.count)print(d.data)

运行结果:

4[‘Letter D‘, ‘Letter B‘, ‘Letter C‘, ‘Letter A‘]

如果不使用super而用非绑定的方式调用父类的方法:

class A():    def add(self):        self.data.append("Letter A")        self.count += 1class B(A):    def add(self):        self.data.append("Letter B")        self.count += 1        A.add(self)class C(A):    def add(self):        self.data.append("Letter C")        self.count += 1        A.add(self)class D(B, C):    def __init__(self):        self.count = 0        self.data = []    def add(self):        self.data.append("Letter D")        self.count += 1        B.add(self)        C.add(self)d = D()d.add()print(d.count)print(d.data)

运行结果:

5[‘Letter D‘, ‘Letter B‘, ‘Letter A‘, ‘Letter C‘, ‘Letter A‘]

d的运行结果多了一个‘’Letter A’,count也加一。

小结:

写了这么多,其实也就可以归结于两点。

没有super以前的涉及到由于修改类继承而产生的修改代码,比较繁琐,用了super之后不用修改。

super可以使你在子类方法中访问父类,祖先类的方法,还能使你以一种有序无重复的方式遍“历”继承树中方法。

Python中super()的使用(一)

相关内容

    暂无相关文章

评论关闭