python中super用法

转:https://blog.csdn.net/u011467044/article/details/52205961

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

python简单的类举例:

[python]  view plain  copy
 
  1. >>> class hello(object):  
  2. ...         def print_c():  
  3. ...             print"hello world!"  
  4. >>> hello().print_c()  
  5. hello world!  

当然在实际中不可避免的需要类的继承,子类继承父类,正常如下:

[python]  view plain  copy
 
  1. >>> class child(hello):  
  2. ...         def print_c(self):  
  3. ...                 hello().print_c()  
  4. ...                   
  5. >>> child().print_c()  
  6. hello world!  

在python中还提供了super()机制,例子如下:

[python]  view plain  copy
 
  1. >>> class hello(object):  
  2. ...         def print_c(self):  
  3. ...             print"hello world!"  
  4. ...               
  5. >>> class child(hello):  
  6. ...         def print_c(self):  
  7. ...                 super(child,self).print_c()  
  8. ...                   
  9. >>> child().print_c()  
  10. hello world!  

第一眼看过来是不是感觉一样?

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

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

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

好,再举一个例子

[python]  view plain  copy
 
  1. class parent1(object):  
  2.     def __init__(self):  
  3.         print 'is parent1'  
  4.         print 'goes parent1'  
  5.   
  6. class parent2(object):  
  7.     def __init__(self):  
  8.         print 'is parent2'  
  9.         print 'goes parent2'  
  10.   
  11. class child1(parent1):  
  12.     def __init__(self):  
  13.         print'is child1'  
  14.         parent.__init__(self)  
  15.         print 'goes child1'  
  16.   
  17. class child2 (parent1) :  
  18.     def __init__(self):  
  19.         print 'is child2'  
  20.         parent.__init__(self)  
  21.         print 'goes child2'  
  22.   
  23. class child3(parent2):  
  24.     def __init__(self):  
  25.         print 'is child3'  
  26.         parent2.__init__(self)  
  27.         print 'goes child3'  
  28.   
  29. class grandson(child3,child2,child1):  
  30.     def __init__(self):  
  31.         print 'is grandson'  
  32.         child1.__init__(self)  
  33.         child2.__init__(self)  
  34.         child3.__init__(self)  
  35.         print'goes grandson'  
  36.   
  37.   
  38. if __name__=='__main__':  
  39.     grandson()  
[python]  view plain  copy
 
  1. is grandson  
  2. is child1  
  3. is parent1  
  4. goes parent1  
  5. goes child1  
  6. is child2  
  7. is parent1  
  8. goes parent1  
  9. goes child2  
  10. is child3  
  11. is parent2  
  12. goes parent2  
  13. goes child3  
  14. goes grandson  
[python]  view plain  copy
 
  1.   
[python]  view plain  copy
 
  1. 好了,在这儿发现什么问题了没有?对,基类parent1被多次执行,而有时我们只希望公共的类只被执行一次,那么此时我们引入super()机制查看效果:  
[python]  view plain  copy
 
  1. <span style="font-family: Arial, Helvetica, sans-serif;">class parent1(object):</span>  
[python]  view plain  copy
 
  1.     def __init__(self):  
  2.         super(parent1, self).__init__()  
  3.         print 'is parent1'  
  4.         print 'goes parent1'  
  5.   
  6. class parent2(object):  
  7.     def __init__(self):  
  8.         super(parent2, self).__init__()  
  9.         print 'is parent2'  
  10.         print 'goes parent2'  
  11.   
  12. class child1(parent1):  
  13.     def __init__(self):  
  14.         print'is child1'  
  15.         #parent1.__init__(self)  
  16.         super(child1, self).__init__()  
  17.         print 'goes child1'  
  18.   
  19. class child2 (parent1) :  
  20.     def __init__(self):  
  21.         print 'is child2'  
  22.         #parent1.__init__(self)  
  23.         super(child2, self).__init__()  
  24.         print 'goes child2'  
  25.   
  26. class child3(parent2):  
  27.     def __init__(self):  
  28.         print 'is child3'  
  29.         #parent2.__init__(self)  
  30.         super(child3, self).__init__()  
  31.         print 'goes child3'  
  32.   
  33. class grandson(child3,child2,child1):  
  34.     def __init__(self):  
  35.         print 'is grandson'  
  36.         #child1.__init__(self)  
  37.         #child2.__init__(self)  
  38.         #child3.__init__(self)  
  39.         super(grandson, self).__init__()  
  40.           
  41.         print'goes grandson'  
  42.   
  43.   
  44. if __name__=='__main__':  
  45.     grandson()  
 
[python]  view plain  copy
 
  1. 此时我们查看结果:  
[python]  view plain  copy
 
  1. is grandson  
  2. is child3  
  3. is child2  
  4. is child1  
  5. is parent1  
  6. goes parent1  
  7. goes child1  
  8. goes child2  
  9. is parent2  
  10. goes parent2  
  11. goes child3  
  12. goes grandson  

结果很明显,公共基类parent1只被执行一次。

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

[python]  view plain  copy
 
  1.    object  
  2.        |  
  3.      /   \  
  4.   P1    P2  
  5. /     \      |  
  6.    C2  C3  
[python]  view plain  copy
 
  1.   

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

后续总结:

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

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

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

猜你喜欢

转载自www.cnblogs.com/shmily2018/p/9073880.html
今日推荐