Python学习笔记(4)Python中super的用法

来源:互联网 发布:暗影格斗2mac破解 编辑:程序博客网 时间:2024/05/29 09:55
super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。总之前人留下的经验就是:保持一致性。要不全部用类名调用父类,要不就全部用 super,不要一半一半。

普通继承
『代码』
[python]view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. class FooParent(object):  
  2.     def __init__(self):  
  3.         self.parent = 'I\'m the parent.'  
  4.         print 'Parent'  
  5.       
  6.     def bar(self,message):  
  7.         print message, 'from Parent'  
  8.           
  9. class FooChild(FooParent):  
  10.     def __init__(self):  
  11.         FooParent.__init__(self)  
  12.         print 'Child'  
  13.           
  14.     def bar(self,message):  
  15.         FooParent.bar(self,message)  
  16.         print 'Child bar function.'  
  17.         print self.parent  
  18.           
  19. if __name__=='__main__':  
  20.     fooChild = FooChild()  
  21.     fooChild.bar('HelloWorld')  


super继承

『代码』

[python]view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. class FooParent(object):  
  2.     def __init__(self):  
  3.         self.parent = 'I\'m the parent.'  
  4.         print 'Parent'  
  5.       
  6.     def bar(self,message):  
  7.         print message,'from Parent'  
  8.   
  9. class FooChild(FooParent):  
  10.     def __init__(self):  
  11.         super(FooChild,self).__init__()  
  12.         print 'Child'  
  13.           
  14.     def bar(self,message):  
  15.         super(FooChild, self).bar(message)  
  16.         print 'Child bar fuction'  
  17.         print self.parent  
  18.   
  19. if __name__ == '__main__':  
  20.     fooChild = FooChild()  
  21.     fooChild.bar('HelloWorld')  

程序运行结果相同,为:

Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

从运行结果上看,普通继承和super继承是一样的。但是其实它们的内部运行机制不一样,这一点在多重继承时体现得很明显。在super机制里可以保证公共父类仅被执行一次,至于执行的顺序,是按照mro进行的(E.__mro__)。
注意super继承只能用于新式类,用于经典类时就会报错。
新式类:必须有继承的类,如果没什么想继承的,那就继承object
经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj』
0 0
原创粉丝点击