Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

python - Python中旧样式类与新样式类有什么区别?(What is the difference between old style and new style classes in Python?)

What is the difference between old style and new style classes in Python?

(Python中旧样式类与新样式类有什么区别?)

When should I use one or the other?

(什么时候应该使用其中一个?)

  ask by Readonly translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes :

(从http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes :)

Up to Python 2.1, old-style classes were the only flavour available to the user.

(直到Python 2.1,旧式类才是供用户使用的唯一样式。)

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'> .

((旧式)类的概念与类型的概念无关:如果x是旧式类的实例,则x.__class__指定x的类,但type(x)始终是<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.

(这反映了这样一个事实,即所有旧式实例(独立于其类)均使用称为实例的单个内置类型实现。)

New-style classes were introduced in Python 2.2 to unify the concepts of class and type .

(在Python 2.2中引入了新型类,以统一类和类型的概念 。)

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中的新型样式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...