python2中新式类和旧式类的对比【译】
Classes and instances come in two flavors: old-style (or classic) and new-style.
➤类和实例分为两大类:旧式类和新式类。
Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x, but type(x) is always <type 'instance'>. This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance.
➤截止到python2.1,只存在旧式类。旧式类中,类名和type是无关的:如果x是一个旧式类,那么x.__class__
定义了x的类名,但是type(x)
总是返回<type 'instance'>
。这反映了所有的旧式类的实例是通过一个单一的叫做instance
的内建类型来实现的,这是它和类不同的地方。
New-style classes were introduced in Python 2.2 to unify classes and types. A new-style class is neither more nor less than a user-defined type. 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__).
➤新式类是在python2.2为了统一类和实例引入的。一个新式类只能由用户自定义。如果x是一个新式类的实例,那么type(x)
和x.__class__
是一样的结果(尽管这不能得到保证,因为新式类的实例的__class__
方法是允许被用户覆盖的)。
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.
➤引入新式类的主要动机是提供一个具有full meta-model(?)的统一对象模型。当然它也有很多实用的功能,比如继承了大部分的内建类型,引入了“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.
➤因为兼容性的原因,类依然是默认的旧式类。新式类只能通过继承另一个新式类或所有类的根类object
来创建(如果没有其他类需要继承的话)。新式类的表现在type()
函数的返回值上有几个和旧式类不同的地方。其中有一些对于新对象模型来说是根本的变化,比如调用了特殊的方法。其他的是作了一些“修正”,那些在考虑兼容性之前不太可能被用到的地方,比如多重继承的方法的区分逻辑。
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 http://www.python.org/doc/newstyle/ for sources of additional information.
Old-style classes are removed in Python 3, leaving only the semantics of new-style classes.
➤旧式类在python3中被移除,只留下了新式类。
python2中新式类和旧式类的对比【译】的更多相关文章
- python中的新式类与旧式类
在python2中,有新式类与旧式类的区别: 首先创建一个类: class Sb(object): pass 如果创建时继承自object,说明这是一个新式类,不写object,说明是一个旧式类: 那 ...
- python新式类与旧式类
python2.X是经典类[旧式类]: 是以深度优先[] 但是在Python2.x中,默认都是经典类,只有显式继承了object才是新式类,即:class Person(object):pass 新式 ...
- Python - 面向对象编程 - 新式类和旧式类
object object 是 Python 为所有对象提供的父类,默认提供一些内置的属性.方法:可以使用 dir 方法查看 新式类 以 object 为父类的类,推荐使用 在 Python 3.x ...
- Python新式类和旧式类的区别
新式类是为了统一**而在2.2中开始引入的. 代码讲解 上面的例子比较明白的说明了问题. B是定义的新式类.那么输入b的时候,不论是type(b),还是b.__class__都是输出的<clas ...
- python(十四)新式类和旧式类
这里有个重要概念呢在下面那个链接 http://blog.csdn.net/zimou5581/article/details/53053775 http://www.cnblogs.com/btch ...
- python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)
s12-20160227-day07 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- python2中新式类和经典类的多重继承调用顺序
class A: def foo(self): print('called A.foo()') class B(A): pass class C(A): def foo(self): print('c ...
- 关于 python 新式类和旧式类继承顺序的验证
参考:http://www.cnblogs.com/blackmatrix/p/5630515.html 官方:https://docs.python.org/2/tutorial/classes.h ...
- python 零散记录(七)(下) 新式类 旧式类 多继承 mro 类属性 对象属性
python新式类 旧式类: python2.2之前的类称为旧式类,之后的为新式类.在各自版本中默认声明的类就是各自的新式类或旧式类,但在2.2中声明新式类要手动标明: 这是旧式类为了声明为新式类的方 ...
随机推荐
- PIC EEPROM问题
1.通过export出来的Hex烧录,EEPROM内容会根据Hex中关于EEPROM的定义而改变. 2.通过编译源文件烧录,如果没有勾选Preserve EEPROM on program则EEPRO ...
- VFS文件系统结构分析 与socket
本文乃fireaxe原创,使用GPL发布,可以自由拷贝,转载.但转载请保持文档的完整性,并注明原作者及原链接.内容可任意使用,但对因使用该内容引起的后果不做任何保证. 作者:fireaxe_hq@ho ...
- 图片无损放大工具PhotoZoom如何进行打印设置
我们使用PhotoZoom对照片进行无失真放大后,想将照片给打印出来需要设置一些常规参数时.那么这些参数我们该从哪里设置,怎么设置呢? PhotoZoom下载:pan.baidu.com/s/1cXb ...
- jsoup解析页面
package com.java.jsoup; /** * jsoup解析网页 * @author nidegui * @version 2019年4月29日 下午5:12:02 * */ impor ...
- C语言提高 (1) 第一天 数据类型本质与内存四区
(物联网的分层的概念 b/s c/s 结构 习惯: 在C语言 0 函数执行成功 <0是错误 >1做一些返回值处理 3 课前准备 工作经验,记录 4 数据类型的本质 数据类型的本质是固定大小 ...
- 分析Ajax来爬取今日头条街拍美图并保存到MongDB
前提:.需要安装MongDB 注:因今日投票网页发生变更,如下代码不保证能正常使用 #!/usr/bin/env python #-*- coding: utf-8 -*- import json i ...
- Ubuntu中的Docker搭建Tensorflow环境
一.docker环境安装 1)更新.安装依赖包 sudo apt-get update sudo apt-get install apt-transport-https ca-certificates ...
- Anaconda安装tensorflow遇到的问题(转)
tensorflow安装教程 1.找不到Anaconda Prompt 其他教程中说AnacondaPrompt在安装的Anaconda文件夹下面(如D:/Anaconda),但是我在安装中没有找到, ...
- BA-BACnet对象
BACNET协议有多少个对象呢,拿出西门子教程中的看看一下,居然有48个,其中的大部分都没有用到:
- CF922B Magic Forest
CF922B Magic Forest 题意翻译 题目大意 给定一个正整数nn ,求满足如下条件的三元组(a,b,c)(a,b,c) 的个数: 1 \le a \le b \le c \le n1≤a ...