python中super用法研究

python语言与C++有相似的类继承,在类定义时,python中会自定义第一个self,类似C++中this指针,指向对象自身。

python简单的类举例:

>>> class hello(object):
...         def print_c():
...             print"hello world!"
>>> hello().print_c()
hello world!
当然在实际中不可避免的需要类的继承,子类继承父类,正常如下:

>>> class child(hello):
...         def print_c(self):
...                 hello().print_c()
...                 
>>> child().print_c()
hello world!
在python中还提供了super()机制,例子如下:

>>> class hello(object):
...         def print_c(self):
...             print"hello world!"
...             
>>> class child(hello):
...         def print_c(self):
...                 super(child,self).print_c()
...                 
>>> child().print_c()
hello world!
第一眼看过来是不是感觉一样?

在python中引入super()的目的是保证相同的基类只初始化一次(注意:

1super ()机制是用来解决多重继承的,对于直接调用父类名是没有问题的,但在之后根据前人的经验就是:要么都用类名调用,要么就全部用super(),不要混合的用,由此为人做事还是要专一的嘛O(∩_∩)O~

super()继承只能用于新式类,用于经典类时就会报错。
新式类:必须有继承的类,如果无继承的,则继承object
经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj

好,再举一个例子

class parent1(object):
    def __init__(self):
        print 'is parent1'
        print 'goes parent1'

class parent2(object):
    def __init__(self):
        print 'is parent2'
        print 'goes parent2'

class child1(parent1):
    def __init__(self):
        print'is child1'
        parent.__init__(self)
        print 'goes child1'

class child2 (parent1) :
    def __init__(self):
        print 'is child2'
        parent.__init__(self)
        print 'goes child2'

class child3(parent2):
    def __init__(self):
        print 'is child3'
        parent2.__init__(self)
        print 'goes child3'

class grandson(child3,child2,child1):
    def __init__(self):
        print 'is grandson'
        child1.__init__(self)
        child2.__init__(self)
        child3.__init__(self)
        print'goes grandson'


if __name__=='__main__':
    grandson()
is grandson
is child1
is parent1
goes parent1
goes child1
is child2
is parent1
goes parent1
goes child2
is child3
is parent2
goes parent2
goes child3
goes grandson

好了,在这儿发现什么问题了没有?对,基类parent1被多次执行,而有时我们只希望公共的类只被执行一次,那么此时我们引入super()机制查看效果:
<span style="font-family: Arial, Helvetica, sans-serif;">class parent1(object):</span>
    def __init__(self):
        super(parent1, self).__init__()
        print 'is parent1'
        print 'goes parent1'

class parent2(object):
    def __init__(self):
        super(parent2, self).__init__()
        print 'is parent2'
        print 'goes parent2'

class child1(parent1):
    def __init__(self):
        print'is child1'
        #parent1.__init__(self)
        super(child1, self).__init__()
        print 'goes child1'

class child2 (parent1) :
    def __init__(self):
        print 'is child2'
        #parent1.__init__(self)
        super(child2, self).__init__()
        print 'goes child2'

class child3(parent2):
    def __init__(self):
        print 'is child3'
        #parent2.__init__(self)
        super(child3, self).__init__()
        print 'goes child3'

class grandson(child3,child2,child1):
    def __init__(self):
        print 'is grandson'
        #child1.__init__(self)
        #child2.__init__(self)
        #child3.__init__(self)
        super(grandson, self).__init__()
        
        print'goes grandson'


if __name__=='__main__':
    grandson()



 
此时我们查看结果:
is grandson
is child3
is child2
is child1
is parent1
goes parent1
goes child1
goes child2
is parent2
goes parent2
goes child3
goes grandson
结果很明显,公共基类parent1只被执行一次。

grandson类的继承体系如下图所示:

       object
           |
         /   \
      P1    P2
    /     \      |
  C1   C2  C3
 
所以对于类的继承体系,我们可以看做一个图,而每一个类看做一个节点,那么super()机制的执行顺序是按照图的广度优先搜索算法查找super()的父类。 

后续总结:

  1. super()是一个类名而非函数,super(class, self)事实上调用了super类的初始化函数,产生了一个super对象;

        2 super()机制必须要用新式类,否则会报错;

        3 super()或直接父类调用最好只选择一种形式。







  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值