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中声明新式类要手动标明: 这是旧式类为了声明为新式类的方 ...
随机推荐
- VTK资料收集
使用Qt Creator开发VTK 原文链接:http://blog.csdn.net/numit/article/details/10200507 VTK应用系列:非常强大!非常善良 05-VTK在 ...
- WIN系统查询版本
cmd -> DISM /online /Get-CurrentEdition //查询系统版本 WIN+R -> slmgr.vbs -ipk 查询系统注册信息slmgr.vbs -dl ...
- 自定义View实现圆角化
目的: 1.实现自定义ReleativeLayout圆角化 实现: 1.在res目录中新建attrs.xml文件,自定义属性如下. <?xml version="1.0" e ...
- bzoj 1189: [HNOI2007]紧急疏散evacuate 分层图最大流_拆点_二分
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一 块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是 ...
- elasticsearch元数据
_source元数据 put /test_index/test_type/1 { "test_field1": "test field1", "tes ...
- select的option触发事件
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Tab 切换效果
今天写的两个小效果都是为了测试我写的单参函数,结果还是有点成功的~~此处是不是想发表情包! Tab效果很简单,这里我就不赘述了,直接上代码: html代码: <div class="c ...
- BZOJ 3144 [HNOI2013]切糕 (最大流+巧妙的建图)
题面:洛谷传送门 BZOJ传送门 最大流神题 把点权转化为边权,切糕里每个点$(i,j,k)$向$(i,j,k+1)$连一条流量为$v(i,j,k)$的边 源点$S$向第$1$层的点连边,第$R+1$ ...
- 2019-04-03 Anaconda+VSCode搭建python开发环境,并连接GIthub
1.最好的Python开发环境 :Anaconda+VSCode搭建python开发环境,conda提供了python开发环境和大量的你不用安装的库 conda的环境变量: 直接在conda 中下载启 ...
- 在使用SSH+JPA开发中,ajax使用ObjectMapper类从后台向前台传值
使用ObjectMapper对象的writeValue方法 ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValu ...