python新式类和经典类的区别实例分析


Posted in Python onMarch 23, 2020

本文实例讲述了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

粘贴一段官网上的作者解释

python新式类和经典类的区别实例分析

是说经典类中如果都有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 相关文章推荐
Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程
Dec 27 Python
python 根据时间来生成唯一的字符串方法
Jan 14 Python
详解Python做一个名片管理系统
Mar 14 Python
python微信撤回监测代码
Apr 29 Python
Python进阶之使用selenium爬取淘宝商品信息功能示例
Sep 16 Python
Python numpy线性代数用法实例解析
Nov 15 Python
浅谈PyQt5中异步刷新UI和Python多线程总结
Dec 13 Python
python获取引用对象的个数方式
Dec 20 Python
python enumerate内置函数用法总结
Jan 07 Python
pycharm部署、配置anaconda环境的教程
Mar 24 Python
Python flask框架如何显示图像到web页面
Jun 03 Python
python判断元素是否存在的实例方法
Sep 24 Python
Python count函数使用方法实例解析
Mar 23 #Python
使用python实现飞机大战游戏
Mar 23 #Python
如何在Django中使用聚合的实现示例
Mar 23 #Python
python3注册全局热键的实现
Mar 22 #Python
浅谈Python线程的同步互斥与死锁
Mar 22 #Python
Django 项目布局方法(值得推荐)
Mar 22 #Python
python实现吃苹果小游戏
Mar 21 #Python
You might like
实用函数7
2007/11/08 PHP
PHP5.3.1 不再支持ISAPI
2010/01/08 PHP
PHP autoload与spl_autoload自动加载机制的深入理解
2013/06/05 PHP
探讨:php中在foreach中使用foreach ($arr as &$value) 这种类型的解释
2013/06/24 PHP
smarty内置函数{loteral}、{ldelim}和{rdelim}用法实例
2015/01/22 PHP
基于php实现随机合并数组并排序(原排序)
2015/11/26 PHP
thinkphp实现分页显示功能
2016/12/03 PHP
dreamweaver 安装Jquery智能提示
2011/04/02 Javascript
jquery ajax例子返回值详解
2012/09/11 Javascript
js的alert弹出框出现乱码解决方案
2013/09/02 Javascript
js为什么不能正确处理小数运算?
2015/12/29 Javascript
jquery模拟实现鼠标指针停止运动事件
2016/01/12 Javascript
JQuery 的跨域方法推荐_可跨任何网站
2016/05/18 Javascript
Bootstrap 源代码分析(未完待续)
2016/08/17 Javascript
nodejs中解决异步嵌套循环和循环嵌套异步的问题
2017/07/12 NodeJs
jQuery DOM节点的遍历方法小结
2017/08/15 jQuery
vue2 router 动态传参,多个参数的实例
2017/11/10 Javascript
js中Object.defineProperty()方法的不详解
2018/07/09 Javascript
单页面vue引入百度统计的使用方法示例详解
2018/10/13 Javascript
详解小程序如何避免多次点击,重复触发事件
2019/04/08 Javascript
Angular6使用forRoot() 注册单一实例服务问题
2019/08/27 Javascript
[01:32]DOTA2 2015国际邀请赛中国区预选赛第四日战报
2015/05/29 DOTA
haskell实现多线程服务器实例代码
2013/11/26 Python
Python的多态性实例分析
2015/07/07 Python
python 实时遍历日志文件
2016/04/12 Python
Python常用的内置序列结构(列表、元组、字典)学习笔记
2016/07/08 Python
python 创建弹出式菜单的实现代码
2017/07/11 Python
APIStar:一个专为Python3设计的API框架
2018/09/26 Python
python使用ctypes调用扩展模块的实例方法
2020/01/28 Python
Django的CVB实例详解
2020/02/10 Python
参观接待方案
2014/03/17 职场文书
静心口服夜广告词
2014/03/20 职场文书
会计专业个人自我鉴定
2014/03/21 职场文书
运动会广播稿50字
2015/08/19 职场文书
Nginx+Tomcat实现负载均衡、动静分离的原理解析
2021/03/31 Servers
Oracle11g r2 卸载干净重装的详细教程(亲测有效已重装过)
2021/06/04 Oracle