本文实例讲述了python新式类和经典类的区别。分享给大家供大家参考,具体如下:
新式类就是 class person(object): 这种形式的, 从py2.2 开始出现的
新式类添加了:
__name__ is the attribute's name.
__doc__ is the attribute's docstring.
__get__(object) is a method that retrieves the attribute value from object.
__set__(object, value) sets the attribute on object to value.
__delete__(object, value) deletes the value attribute of object.
新式类的出现, 除了添加了大量方法以外, 还改变了经典类中一个多继承的bug, 因为其采用了广度优先的算法
Python 2.x中默认都是经典类,只有显式继承了object才是新式类
python 3.x中默认都是新式类,经典类被移除,不必显式的继承object
粘贴一段官网上的作者解释
是说经典类中如果都有save方法, C中重写了save() 方法, 那么寻找顺序是 D->B->A, 永远找不到C.save()
代码演示:
#!/usr/bin/env python3 #coding:utf-8 ''' 新式类和经典类的区别, 多继承代码演示 ''' class A: def __init__(self): print 'this is A' def save(self): print 'save method from A' class B: def __init__(self): print 'this is B' class C: def __init__(self): print 'this is c' def save(self): print 'save method from C' class D(B, C): def __init__(self): print 'this is D' d = D() d.save()
结果显示
this is D
save method from C
注意: 在python3 以后的版本中, 默认使用了新式类, 是不会成功的
另外: 经典类中所有的特性都是可读可写的, 新式类中的特性只读的, 想要修改需要添加 @Texing.setter
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】