关于 python 新式类和旧式类继承顺序的验证

参考:http://www.cnblogs.com/blackmatrix/p/5630515.html

官方:https://docs.python.org/2/tutorial/classes.html

关于多继承问题

Python supports a limited form of multiple inheritance as well. A class definition with multiple base classes looks like this:

For old-style classes, the only rule is depth-first, left-to-right. Thus, if an attribute is not found in DerivedClassName, it is searched in Base1, then (recursively) in the base classes of Base1, and only if it is not found there, it is searched in Base2, and so on.

(To some people breadth first — searching Base2 and Base3 before the base classes of Base1 — looks more natural. However, this would require you to know whether a particular attribute of Base1 is actually defined in Base1 or in one of its base classes before you can figure out the consequences of a name conflict with an attribute of Base2. The depth-first rule makes no differences between direct and inherited attributes of Base1.)

For new-style classes, the method resolution order changes dynamically to support cooperative calls to super(). This approach is known in some other multiple-inheritance languages as call-next-method and is more powerful than the super call found in single-inheritance languages.

试验部分

请看这里

以上.

时间: 2024-08-14 06:46:26

关于 python 新式类和旧式类继承顺序的验证的相关文章

python 新式类和旧式类

新式类和旧式类 python的新式类是2.2版本引进来的,我们可以将之前的类叫做经典类或者旧类. 为什么要在2.2中引进new style class呢?官方给的解释是: 为了统一类(class)和类型(type). 在2.2之前,比如2.1版本中,类和类型是不同的,如a是ClassA的一个实例,那么a.__class__返回 ' class    __main__.ClassA' ,type(a)返回总是<type 'instance'>.而引入新类后,比如ClassB是个新类,b是Clas

python新式类与旧式类

python2.X是经典类[旧式类]: 是以深度优先[] 但是在Python2.x中,默认都是经典类,只有显式继承了object才是新式类,即:class Person(object):pass 新式类写法class Person():pass 经典类写法class Person:pass 经典类写法 他们最明显的区别在于继承搜索的顺序发生了改变,即经典类多继承搜索顺序(深度优先):先深入继承树左侧查找,然后再返回,开始查找右侧,如图所示: 1           A 2-1 B       

Python新式类和旧式类的区别

新式类是为了统一**而在2.2中开始引入的. 代码讲解 上面的例子比较明白的说明了问题. B是定义的新式类.那么输入b的时候,不论是type(b),还是b.__class__都是输出的<class '__main__.B'>. A是定义的旧式类. ----------------------------------- 另外,新式类和旧式类还有一个区别就是在多继承的时候,查找要调用的方法. 新式类是广度优先的查找算法. 旧式类的查找方法是深度优先的.

python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)

s12-20160227-day07 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin

【面向对象】新式类和旧式类的区别

旧式类: class jiushi(): def __init__(self,a1): self.a=a1 class jiushi_son(jiushi): def __init__(self,a1): jiushi.__init__(self,a1) 新式类:(多继承时) class xinshi(object): def __init__(self, a1): self.a = a1 class xinshi_son(xinshi): def __init__(self, a1): sup

python 之简单浅谈新式类和旧式类(或称经典类)

#_*_coding:utf-8 _*_ #知识点:经典类和新式类的区别 #父类 class Father(object):     def __init__(self):         self.Fname = 'fffffff'         print 'father.__init__'     def Func(self):         print 'funcfurnc'     def Bar(self):         print 'barbarbar'     def T

【Python】Python 新式类介绍

本文转载自:kaka_ace's blog 我们使用 Python 开发时, 会遇到 class A 和 class A(object) 的写法, 这在 Python2 里是有概念上和功能上的区别, 即经典类(旧式类)与新式类的区别, 英文上分别描述为 old-style(classic-style) 与 new-style. 通过搜索, 先查阅了三个资料链接: 官方文档 stackoverflow 解答 Python Types and Objects 根据 stackoverflow 答案引

Python新式类 单例模式与作用域(四)

1 新式类与旧式类 新式类拥有经典类的全部特性之外,还有一些新的特性,比如 __init__发生变化,新增了静态方法__new__,python3目前都采用新式类,新式类是广度优先,旧式类是深度优先 #新式类 class C(object): pass #经典类 class B: pass (1)内置的object对象 1. __new__,__init__方法 这两个方法是用来创建object的子类对象,静态方法__new__()用来创建类的实例,然后再调用 __init__()来初始化实例.

Python新式类与经典类的区别

1.新式类与经典类 在Python 2及以前的版本中,由任意内置类型派生出的类(只要一个内置类型位于类树的某个位置),都属于“新式类”,都会获得所有“新式类”的特性:反之,即不由任意内置类型派生出的类,则称之为“经典类”. “新式类”和“经典类”的区分在Python 3之后就已经不存在,在Python 3.x之后的版本,因为所有的类都派生自内置类型object(即使没有显示的继承object类型),即所有的类都是“新式类”. 官方文档 https://www.python.org/doc/new