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 . 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)总是返回。这反映了所有的旧式类的实例是通过一个单一的叫做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中被移除,只留下了新式类。