Python之路--super关键字

来源:互联网 发布:java视频压缩算法 编辑:程序博客网 时间:2024/05/28 16:17

python之路–super关键字

最近遇到了python类的继承,要想让代码贯彻pythonic的简洁风范,super或许是你的一个不错的选择,官方文档里对super的介绍在这儿 [Built-in Functions — Python 2.7.12 documentation][1]

普通继承

python的继承实现起来非常简单:

class A:    passclass B(A):    pass

如果父类定义了__init__方法,那么子类必须显示的调用父类的__init__方法:

class A:    def __init__(self):        print "I'm A"class B(A):    def __init__(self):        A.__init__(self):

>>>test = B()
I’m A

super继承

class A(object):    def __init__(self):        print "I'm A"class B(A):    def __init__(self):        super(B, self).__init__(self)

>>>test = B()
I’m A

注意这里的几点不同:

  • 1,父类A必须继承于object,即父类A必须为新式类
  • 2,继承模式为
    class ClassName(B):        def method(self, arg):            super(ClassName, self).method(arg)

为什么要用super?

在上面的例子中,super并没有体现什么优越性,甚至还增加了对父类的限制。但在下面的样例中,super的个性就凸显出来了。

多类继承

普通继承

class A(object):    def __init__(self):        print "Enter A"        print "Leave A"class B(A):    def __init__(self):        print "Enter B"        A.__init__(self)        print "Leave B"class C(A):    def __init__(self):        print "Enter C"        A.__init__(self)        print "Leave C"class D(B, C):    def __init__(self):        print "Enter D"        B.__init__(self)        C.__init__(self)        print "Leave D"

>>>test = D()
Enter D
Enter B
Enter A
Leave A
Leave B
Enter C
Enter A
Leave A
Leave C
Leave D

super继承

class A(object):    def __init__(self):        print "Enter A"        print "Leave A"class B(A):    def __init__(self):        print "Enter B"        super(B, self).__init__()        print "Leave B"class C(A):    def __init__(self):        print "Enter C"        super(C, self).__init__()        print "Leave C"class D(B, C):    def __init__(self):        print "Enter D"        super(D, self).__init__()        print "Leave D"

>>>test = D()
Enter D
Enter B
Enter C
Enter A
Leave A
Leave C
Leave B
Leave D

对比以上两种方式及其结果,我们发现:
- super在针对多类继承时语法更为简洁,不管是几类继承,只需要一个super(ClassName, self).method(arg)就可以了,这一点优势将在你的继承父类可能变化,而你的代码量又比较庞大时迅速凸显出来,试想别的程序员在小心翼翼的寻找代码改动时,你只需要改动一行(甚至不需要改,在后面会谈到),这是一件多么美妙的事。
- 我们还看到在普通多重继承时,公共父类A被执行了两次,而在super继承时,公共父类A只被执行了一次,这是由于python的MRO(Method Resolution Order)机制造成的,对MRO机制的详细介绍我们会在以后给出。

==========================================
未完待续

参考资料:
https://docs.python.org/2/library/functions.html?highlight=super#super
https://www.python.org/download/releases/2.3/mro/

2 0