Python中super的用法
2015-06-17 16:01
711 查看
Python中super的用法
分类: python2014-05-2117:50 6637人阅读 评论(0) 收藏 举报
super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。总之前人留下的经验就是:保持一致性。要不全部用类名调用父类,要不就全部用 super,不要一半一半。
普通继承
『代码』
[python]view
plaincopy
class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print 'Parent'
def bar(self,message):
print message, 'from Parent'
class FooChild(FooParent):
def __init__(self):
FooParent.__init__(self)
print 'Child'
def bar(self,message):
FooParent.bar(self,message)
print 'Child bar function.'
print self.parent
if __name__=='__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
super继承
『代码』
[python]view
plaincopy
class FooParent(object):
def __init__(self):
self.parent = 'I\'m the parent.'
print 'Parent'
def bar(self,message):
print message,'from Parent'
class FooChild(FooParent):
def __init__(self):
super(FooChild,self).__init__()
print 'Child'
def bar(self,message):
super(FooChild, self).bar(message)
print 'Child bar fuction'
print self.parent
if __name__ == '__main__':
fooChild = FooChild()
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』
关于super用法的详细研究可参考「http://blog.csdn.net/johnsonguo/article/details/585193」
转自:http://blog.csdn.net/lqhbupt/article/details/19631991
相关文章推荐
- Python特殊语法:filter、map、reduce、lambda [转]
- Python Web框架Django (五)
- 如何使用Python 打开exel等各种文件或者应用,就用:os.startfile
- PEP 0008 -- Style Guide for Python Code
- Python实现的彩票机选器实例
- Python程序员的进化过程
- Python的数据格式动态标记格式
- [Python & Machine Learning] 学习笔记之scikit-learn机器学习库
- 在python安装基础上两步命令安装Gensim
- Python日志模块logging格式化封装
- Python Interview Question and Answers
- Python小知识
- 在python中对list求和及求积
- Python基于Tkinter实现的记事本实例
- notepad++设置用空格替换Tab键
- Python基于Tkinter的HelloWorld入门实例
- 在Python中使用glob模块查找文件路径的方法
- 在Python的struct模块中进行数据格式转换的方法
- python 汉字与拼音的转换--pypinyin
- Python Web框架Django (四)