本文转载自:kaka_ace's blog

我们使用 Python 开发时, 会遇到 class A 和 class A(object) 的写法,
这在 Python2 里是有概念上和功能上的区别, 即经典类(旧式类)与新式类的区别,
英文上分别描述为 old-style(classic-style) 与 new-style.

通过搜索, 先查阅了三个资料链接:
官方文档
stackoverflow 解答
Python Types and Objects

根据 stackoverflow 答案引出的语言发明者 Guido 写的一篇文章:
The Inside Story on New-Style Classes
其总结可以作为官方解释:


  • low-level constructors named __new__() – 低级别的构造函数.
    Note: Python 的 class __init__ 并不是其他语言意义上的构造函数,
    在 new 创建实例后对实例属性初始化的函数.

  • descriptors, a generalized way to customize attribute access – 描述符.
    或者说描述符协议支持.descriptor protocol __get__, __set__ ,__delete__ 等,
    可以阅读descriptor 文档

  • static methods and class methods - 静态方法和类方法

  • properties (computed attributes) – 属性访问 setter getter.

  • decorators (introduced in Python 2.4) – 装饰器.
    现在装饰器语法糖遍布各Python框架.

  • slots – 用户设置后可以限定实例的属性.
    在 Python2 中替代 __dict__, 可以节省近 2/3 内存, Python3 中可以
    不因为优化内存使用率而使用 slots, 因为 __dict__ 结构内存做了优化,
    Note: __dict__ 并不是 Python 意义上的内置的 dict, 其实是一个 proxy 类.

  • a new Method Resolution Order (MRO) – MRO 方法解析次序改变
    (由左递归改为C3算法)

另一个答案增加了一点:

  • super added – super 方法支持

关于C3MRO介绍, 可以点击链接

以下是翻译官方文档的资料 (Python2的内容)


Classes and instances come in two flavors: old-style (or classic) and new-style.
类和实例有两种方式: 旧式(或者经典) 和 新式类.

Up to Python 2.1 the concept of class was unrelated to the concept of type,
and old-style classes were the only flavor available. For an old-style class,
the statement x.__class__ provides the class of x, but type(x) is always
<type ‘instance’>. This reflects the fact that all old-style instances, independent
of their class, are implemented with a single built-in type, called instance.

一直到 Python 2.1 class 的概念不与 type 不一样, 旧式类是唯一的风格. 对于一个
旧式类里, 陈述句 x.__class__ 提供 x 的 class, 但是 type(x) 一直是 <type ‘instance’>.
它反应了所有旧式类实例, 独立于它们的 class, 它们通过一个简单的内建 type 实现,
被称作 实例 instance.

New-style classes were introduced in Python 2.2 to unify the concepts of class
and type. A new-style class is simply a user-defined type, no more, no less.
If x is an instance of a new-style class, then type(x) is typically the same as
x.__class__ (although this is not guaranteed – a new-style class instance is
permitted to override the value returned for x.__class__).

新式类在Python 2.2 引入, 用于统一 class 和 type 的概念. 一个新式类简化了用户
自定义的 type, 不多不少. 如果 x 是一个新式类的实例 instance , type(x) 典型地
与 x.__class__ (尽管这并不能保证 – 一个新式类被允许重写 x.__class__ 的值)一致.

eg:
>>> class A(object): pass
>>> a = A()
>>> a.__class__
>>> <class ‘__main__.A’>
>>>
>>> type(a)
>>> <class ‘__main__.A’>

而 class A 的 type 和 __class__ 是 <type ‘type’>

>>> A.__class__
>>> <type ‘type’>

The major motivation for introducing new-style classes is to provide a unified
object model with a full meta-model. It also has a number of practical benefits,
like the ability to subclass most built-in types, or the introduction of
“descriptors”, which enable computed properties.

引入新式类主要的改进是提供统一的带有完整 meta-model 的 object model.
它可以拥有大量的使用价值, 类似于 内建 type的 subclass的能力 或者 描述符
descriptors 介绍(允许使用的计算属性)

For compatibility reasons, classes are still old-style by default. New-style
classes are created by specifying another new-style class (i.e. a type) as a
parent class, or the “top-level type” object if no other parent is
needed. The behaviour of new-style classes differs from that of
old-style classes in
a number of important details in addition to what type() returns. Some of these
changes are fundamental to the new object model, like the way special methods
are invoked. Others are “fixes” that could not be implemented before for
compatibility concerns, like the method resolution order in case of multiple
inheritance.

考虑到兼容的因素, class 使用默认是旧式类. 新式类通过指定的另一个新式类
(如 一个 type)作为父类, 或者是顶级类 object(如果没有其他父类需要)创建.
除了 type() 的返回方式,新式类与旧式类的行为从大量重要细节区分. 这些变化
对于新式类 oject model 是基础性的, 类似如 特别的方法被调用时的方式. 其他
特性属于修复以前不能实现的兼容性问题, 例如多重继承下的 MRO 方法解析次序.

Tips:
1. 旧式类多重继承使用的是左递归遍历方式
2. 新式类多重继承算法使用 C3 算法.

While this manual aims to provide comprehensive coverage of Python’s class
mechanics, it may still be lacking in some areas when it comes to its coverage
of new-style classes. Please see https://www.python.org/doc/newstyle/ for
sources of additional information.

该手册目标是提供综合性地覆盖介绍 Python 的 class 机制, 在新式类的覆盖
范围内, 它可能依然会在某些方面缺少信息. 请阅读 https://www.python.org/doc/newstyle/
来获额外的信息源.

Old-style classes are removed in Python 3, leaving only new-style classes.
旧式类在 Python3 中去除, 只保留新式类.

原创文章,转载请注明: 转载自kaka_ace's blog

参考资料:

  1.http://www.zhihu.com/question/19754936

【Python】Python 新式类介绍的更多相关文章

  1. python中新式类和经典类

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

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

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

  3. Python中新式类 经典类的区别(即类是否继承object)

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

  4. python 面向对象 新式类和经典类

    # 经典类写法 # schoolMember.__init__(self, name, age, sex) # 新式类写法 super(Teather, self).__init__(name, ag ...

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

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

  6. python基础===新式类与经典类

    首先: Python 2.x中默认都是经典类,只有显式继承了object才是新式类 Python 3.x中默认都是新式类,不必显式的继承object 这两种类的区别: 新式类重定义的方法更多,当然这不 ...

  7. python基础--新式类实现单例模式

    在网上看了有关python实现单例模式的博客,发现好多都是转载的,并且都是按照python2.x版本旧式类的方式写的. 虽然也能读懂,但对于我这种一开始学的就是python3.x的新手来说,心里总有点 ...

  8. python之新式类与经典类

    经典类与新式类 经典类:P 或 P()--深度查找,向上查父节点 新式类 :P(object)---广度查找,继承object,新式类的方法较多  

  9. python 3新式类的多继承

    因为我用的是python3,所以所用到的类都是新式类,这里我说的都是新式类,python2类的继承复杂一些,主要有新式类和老式类.python3类(新式类)的继承是是广度优先(BFS),实例如下: c ...

随机推荐

  1. C# 获取UTC 转换时间戳为C#时间

    获取UTC /// <summary> /// 获取时间戳 /// </summary> /// <returns>UTC</returns> publ ...

  2. 北京Uber优步司机奖励政策(3月12日~3月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 【SQLSERVER】如何设置权限用户

    一.设置权限用户的意义 SQLSERVER 数据库有两个登录方式,一个是 Windows 身份验证方式 ,另一个是 SQLSERVER 身份验证方式(sa用户): 1, Windows 身份验证方式, ...

  4. 关联分析FPGrowth算法在JavaWeb项目中的应用

    关联分析(关联挖掘)是指在交易数据.关系数据或其他信息载体中,查找存在于项目集合或对象集合之间的频繁模式.关联.相关性或因果结构.关联分析的一个典型例子是购物篮分析.通过发现顾客放入购物篮中不同商品之 ...

  5. 微信小程序—day04

    元素水平+垂直居中 昨天的用户页的用户头像,是根据已知的像素大小,设置固定的值,达到居中的效果. 今日切换机型进行适配,发现对不同尺寸大小的屏幕不匹配.所以对wxss进行修改,真正达到水平+垂直居中. ...

  6. ISE 14.7安装教程最新版(Win10安装)——解决Win10安装完后打不开快捷方式的方法

    ISE 14.7安装教程最新版(Win10安装) Xilinx ISE是一款世界著名的硬件设计软件,它为设计流程的每一步都提供了直观的生产力增强工具,覆盖从系统级设计探索.软件开发和基于HDL硬件设计 ...

  7. 前端开发工程师 - 04.页面架构 - CSS Reset & 布局解决方案 & 响应式 & 页面优化 &规范与模块化

    04.页面架构 第1章--CSS Reset 第2章--布局解决方案 居中布局 课堂交流区 水平列表的底部对齐 如图所示,一个水平排列的列表,每项高度都未知,但要求底部对齐,有哪些方法可以解决呢? & ...

  8. [Clr via C#读书笔记]Cp7常量和字段

    Cp7常量和字段 常量 常量在编译的时候必须确定,只能一编译器认定的基元类型.被视为静态,不需要static:直接嵌入IL中: 区别ReadOnly 只能在构造的时候初始化,内联初始化. 字段 数据成 ...

  9. 1208: [HNOI2004]宠物收养所

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 12030 Solved: 4916 Description ...

  10. Cassandra 类型转换限制

    原文地址:http://stackoverflow.com/questions/31880381/cassandra-alter-column-type-which-types-are-compati ...