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__
).
(如果x是新型类的实例,则type(x)
通常与x.__class__
相同(尽管不能保证–允许使用新型类实例覆盖为x.__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 immediate benefits, like the ability to subclass most built-in types, or the introduction of "descriptors", which enable computed properties.
(它还具有许多直接的好处,例如能够对大多数内置类型进行子类化,或者引入了“描述符”,以启用计算属性。)
For compatibility reasons, classes are still old-style by default .
(出于兼容性原因,默认情况下,类仍为旧样式 。)
New-style classes are created by specifying another new-style class (ie 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. (其他是出于兼容性考虑而无法实现的“修补程序”,例如在多重继承的情况下的方法解析顺序。)
Python 3 only has new-style classes .
(Python 3仅具有新型类 。)
No matter if you subclass from object
or not, classes are new-style in Python 3.
(无论您是否从object
继承子类,类都是Python 3中的新型样式。)