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 相关文章推荐
python 示例分享---逻辑推理编程解决八皇后
Jul 20 Python
详解Python中with语句的用法
Apr 15 Python
介绍Python的Django框架中的QuerySets
Apr 20 Python
Python实现一个Git日志统计分析的小工具
Dec 14 Python
Python实现采用进度条实时显示处理进度的方法
Dec 19 Python
python解析含有重复key的json方法
Jan 22 Python
python sqlite的Row对象操作示例
Sep 11 Python
flask框架蓝图和子域名配置详解
Jan 25 Python
python如何写个俄罗斯方块
Nov 06 Python
python中使用asyncio实现异步IO实例分析
Feb 26 Python
python3实现Dijkstra算法最短路径的实现
May 12 Python
python 多态 协议 鸭子类型详解
Nov 27 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
使用swoole扩展php websocket示例
2014/02/13 PHP
MongoDB在PHP中的常用操作小结
2014/02/20 PHP
php socket实现的聊天室代码分享
2014/08/16 PHP
PHP中使用addslashes函数转义的安全性原理分析
2014/11/03 PHP
php多线程实现方法及用法实例详解
2015/10/26 PHP
Linux php 中文乱码的快速解决方法
2016/05/13 PHP
Docker 如何布置PHP开发环境
2016/06/21 PHP
PHP实现带重试功能的curl连接示例
2016/07/28 PHP
取得窗口大小 兼容所有浏览器的js代码
2011/08/09 Javascript
jquery 显示*天*时*分*秒实现时间计时器
2014/05/07 Javascript
node.js中的require使用详解
2014/12/15 Javascript
JavaScript检测实例属性, 原型属性
2015/02/04 Javascript
js实现滚动条滚动到某个位置便自动定位某个tr
2021/01/20 Javascript
如何解决IONIC页面底部被遮住无法向上滚动问题
2016/09/06 Javascript
在vscode里使用.vue代码模板的方法
2018/04/28 Javascript
JavaScript实现构造json数组的方法分析
2018/08/17 Javascript
vue+element 模态框表格形式的可编辑表单实现
2019/06/07 Javascript
原生js实现html手机端城市列表索引选择城市
2020/06/24 Javascript
[01:24]2014DOTA2 TI第二日 YYF表示这届谁赢都有可能
2014/07/11 DOTA
在Linux上安装Python的Flask框架和创建第一个app实例的教程
2015/03/30 Python
python实现从ftp服务器下载文件的方法
2015/04/30 Python
DataFrame 将某列数据转为数组的方法
2018/04/13 Python
python3实现网页版raspberry pi(树莓派)小车控制
2020/02/12 Python
用python给csv里的数据排序的具体代码
2020/07/17 Python
Python Web项目Cherrypy使用方法镜像
2020/11/05 Python
python实现定时发送邮件到指定邮箱
2020/12/23 Python
matplotlib绘制鼠标的十字光标的实现(内置方式)
2021/01/06 Python
简洁自适应404页面HTML好看的404源码
2020/12/16 HTML / CSS
挪威太阳镜和眼镜网上商城:SmartBuyGlasses挪威
2016/08/20 全球购物
Laravel的加密解密与哈希实例讲解
2021/03/24 PHP
新书吧创业计划书
2014/01/31 职场文书
个人委托书范文
2015/01/28 职场文书
食堂采购员岗位职责
2015/04/03 职场文书
班干部竞选演讲稿(精选5篇)
2019/09/24 职场文书
聊一聊Redis与MySQL双写一致性如何保证
2021/06/26 Redis
Python卷积神经网络图片分类框架详解分析
2021/11/07 Python