首先什么是新式类 经典类呢:

#新式类是指继承object的类
class A(obect):
...........
#经典类是指没有继承object的类
class A:
...........

Python中推荐大家使用新式类 1.新的肯定好哈,已经兼容经典类

              2.修复了经典类中多继承出现的bug

下面我们着重说一下多继承的bug 如图:

BC 为A的子类, D为BC的子类 ,A中有save方法,C对其进行了重写

在经典类中 调用D的save方法 搜索按深度优先 路径B-A-C, 执行的为A中save 显然不合理
在新式类的 调用D的save方法 搜索按广度优先 路径B-C-A, 执行的为C中save

对深度优先,广度优先 有不解的点这里

#经典类
class A:
def __init__(self):
print 'this is A' def save(self):
print 'come from A' class B(A):
def __init__(self):
print 'this is B' class C(A):
def __init__(self):
print 'this is C'
def save(self):
print 'come from C' class D(B,C):
def __init__(self):
print 'this is D' d1=D()
d1.save() #结果为'come from A
#新式类
class A(object):
def __init__(self):
print 'this is A' def save(self):
print 'come from A' class B(A):
def __init__(self):
print 'this is B' class C(A):
def __init__(self):
print 'this is C'
def save(self):
print 'come from C' class D(B,C):
def __init__(self):
print 'this is D' d1=D()
d1.save() #结果为'come from C'

Python中新式类 经典类的区别(即类是否继承object)的更多相关文章

  1. python中import和from...import...的区别

    python中import和from...import...的区别: 只用import时,如import xx,引入的xx是模块名,而不是模块内具体的类.函数.变量等成员,使用该模块的成员时需写成xx ...

  2. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  3. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  4. python中新式类和经典类的区别

    1).python在类中的定义在py2-3版本上是使用的有新式类和经典类两种情况,在新式类和经典类的定义中最主要的区别是在定义类的时候是否出现引用object;如:经典类:Class 类名::而新式类 ...

  5. Python中新式类和经典类的区别,钻石继承

    1)首先,写法不一样: class A: pass class B(object): 2)在多继承中,新式类采用广度优先搜索,而旧式类是采用深度优先搜索. 3)新式类更符合OOP编程思想,统一了pyt ...

  6. python中新式类和经典类

    python中的类分为新式类和经典类,具体有什么区别呢?简单的说, 1.新式类都从object继承,经典类不需要. Python 2.x中默认都是经典类,只有显式继承了object才是新式类 Pyth ...

  7. Python 中的__new__和__init__的区别

    [同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...

  8. Python中str()与repr()函数的区别

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a ...

  9. python中的__init__和__new__的区别

    一.__init__ 方法是什么?(init前后的线是双下划线) 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例 ...

随机推荐

  1. 对于两个初始时设置为Sensor的刚体,不会触发preSolve和postSolve

    Main.as package{ import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamic ...

  2. Mastering Creativity:A brief guide on how to overcome creative blocks

    MASTERING CREATIVITY, 1st EditionThis guide is free and you are welcome to share it withothers.From ...

  3. Rust语言学习笔记(5)

    Structs(结构体) struct User { username: String, email: String, sign_in_count: u64, active: bool, } let ...

  4. C++17尝鲜:string_view

    string_view string_view 是C++17所提供的用于处理只读字符串的轻量对象.这里后缀 view 的意思是只读的视图. 通过调用 string_view 构造器可将字符串转换为 s ...

  5. (转)2018几大主流的UI/JS框架——前端框架 [Vue.js(目前市场上的主流)]

    https://blog.csdn.net/hu_belif/article/details/81258961 2016年开始应该是互联网飞速发展的几年,同时也是Web前端开发非常火爆的一年,Web ...

  6. 吴裕雄 python深度学习与实践(6)

    from pylab import * import pandas as pd import matplotlib.pyplot as plot import numpy as np filePath ...

  7. yii配置访问路由权限配置

    'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post', 'ge ...

  8. 跨年操作--new Date()

    //时间在2017/12/31 17:00 --- 2018/1/1 06:00区间,提示用户无法操作公告. //time.js (function(){ var date = new Date(); ...

  9. php正则替换双引号里面的字符

  10. surf特征点检测

    ※注:参数SURF中的hessian阈值是图像Hessian矩阵判别式的阈值,值越大检测出的特征点就越少,也就意味着特征点越稳定 #include "opencv2/core/core.hp ...