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实现清屏的方法
Apr 30 Python
利用Python开发实现简单的记事本
Nov 15 Python
Python中模块string.py详解
Mar 12 Python
python with提前退出遇到的坑与解决方案
Jan 05 Python
解决tensorflow模型参数保存和加载的问题
Jul 26 Python
使用python对文件中的单词进行提取的方法示例
Dec 21 Python
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
Apr 26 Python
Django 开发环境配置过程详解
Jul 18 Python
40行Python代码实现天气预报和每日鸡汤推送功能
Feb 27 Python
Python各种扩展名区别点整理
Feb 27 Python
Python接口测试环境搭建过程详解
Jun 29 Python
python实现简单反弹球游戏
Apr 12 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
PHP数据缓存技术
2007/02/14 PHP
Yii2中SqlDataProvider用法示例
2016/09/22 PHP
PHP PDOStatement::fetchObject讲解
2019/02/01 PHP
jquery 表单进行客户端验证demo
2009/08/24 Javascript
jquery 问答知识整理
2010/02/11 Javascript
jquery加载页面的方法(页面加载完成就执行)
2011/06/21 Javascript
获取body标签的两种方法
2011/10/13 Javascript
js/jquery获取浏览器窗口可视区域高度和宽度以及滚动条高度实现代码
2012/12/17 Javascript
jquery简单的拖动效果实现原理及示例
2013/07/26 Javascript
浅析用prototype定义自己的方法
2013/11/14 Javascript
js实现有时间限制消失的图片方法
2015/02/27 Javascript
JavaScript动态修改弹出窗口大小的方法
2015/04/06 Javascript
jQuery中closest和parents的区别分析
2015/05/07 Javascript
jquery实现动态改变div宽度和高度
2015/05/08 Javascript
jQuery实现的分页功能示例
2017/01/22 Javascript
对Angular中单向数据流的深入理解
2018/03/31 Javascript
vue组件之间数据传递的方法实例分析
2019/02/12 Javascript
Python解决N阶台阶走法问题的方法分析
2017/12/28 Python
Python数据分析之双色球基于线性回归算法预测下期中奖结果示例
2018/02/08 Python
python使用ddt过程中遇到的问题及解决方案【推荐】
2018/10/29 Python
pandas pivot_table() 按日期分多列数据的方法
2018/11/16 Python
Python sklearn KFold 生成交叉验证数据集的方法
2018/12/11 Python
Python实现字符型图片验证码识别完整过程详解
2019/05/10 Python
python flask web服务实现更换默认端口和IP的方法
2019/07/26 Python
TensorFlow tensor的拼接实例
2020/01/19 Python
tensorflow 限制显存大小的实现
2020/02/03 Python
python判断变量是否为int、字符串、列表、元组、字典的方法详解
2020/02/13 Python
解析Tensorflow之MNIST的使用
2020/06/30 Python
Python爬虫教程知识点总结
2020/10/19 Python
大学四年的个人自我评价
2014/01/14 职场文书
培训科主任岗位职责
2014/08/08 职场文书
工伤劳动仲裁代理词
2015/05/25 职场文书
硕士毕业答辩开场白
2015/05/27 职场文书
酒店温馨提示语
2015/07/14 职场文书
git中cherry-pick命令的使用教程
2022/06/25 Servers
windows系统安装配置nginx环境
2022/06/28 Servers