Python 中旧式类和新式类有什么区别?
Python 中旧式类和新式类有什么区别? 我什么时候应该使用其中之一?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Python 中旧式类和新式类有什么区别? 我什么时候应该使用其中之一?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
来自新式和经典类< /a>:
From New-style and classic classes:
声明方式:
新式类继承自object,或其他新式类。
旧式课程则不然。
Python 3 注意:
Python 3 不支持旧样式类,因此上述任何一种形式都会生成新样式类。
Declaration-wise:
New-style classes inherit from object, or from another new-style class.
Old-style classes don't.
Python 3 Note:
Python 3 doesn't support old style classes, so either form noted above results in a new-style class.
新旧样式类之间的重要行为变化
Exception
(下面的示例)__slots__
添加了MRO(方法解析顺序)已更改
在其他答案中提到过,但这里有一个经典MRO和C3 MRO(在新样式类中使用)之间区别的具体示例。
问题是在多重继承中搜索属性(包括方法和成员变量)的顺序。
经典类从左到右进行深度优先搜索。 停在第一场比赛上。 它们没有
__mro__
属性。新式类 MRO 在单个英语句子中综合起来更加复杂。 此处对此进行了详细解释。 它的属性之一是,只有在搜索完基类的所有派生类后,才会搜索基类。 它们具有显示搜索顺序的 __mro__ 属性。
除非从
Exception
派生,否则无法引发新样式类对象。在 Python 2.5 左右,可以引发许多类,而在 Python 2.6 左右,这一点已被删除。 在 Python 2.7.3 上:
Important behavior changes between old and new style classes
Exception
(example below)__slots__
addedMRO (Method Resolution Order) changed
It was mentioned in other answers, but here goes a concrete example of the difference between classic MRO and C3 MRO (used in new style classes).
The question is the order in which attributes (which include methods and member variables) are searched for in multiple inheritance.
Classic classes do a depth-first search from left to right. Stop on the first match. They do not have the
__mro__
attribute.New-style classes MRO is more complicated to synthesize in a single English sentence. It is explained in detail here. One of its properties is that a base class is only searched for once all its derived classes have been. They have the
__mro__
attribute which shows the search order.New style class objects cannot be raised unless derived from
Exception
Around Python 2.5 many classes could be raised, and around Python 2.6 this was removed. On Python 2.7.3:
旧式类的属性查找速度仍然稍快一些。 这通常并不重要,但在性能敏感的 Python 2.x 代码中可能很有用:
Old style classes are still marginally faster for attribute lookup. This is not usually important, but it may be useful in performance-sensitive Python 2.x code:
Guido 写了内部故事Story on New-Style Classes,一篇关于 Python 中新式和旧式类的非常好的文章。
Python 3 只有新式类。 即使您编写了一个“旧式类”,它也是隐式地从
object
派生的。新式类具有旧式类所缺乏的一些高级功能,例如
super
、新的 C3 mro,一些神奇的方法等等。Guido has written The Inside Story on New-Style Classes, a really great article about new-style and old-style class in Python.
Python 3 has only new-style class. Even if you write an 'old-style class', it is implicitly derived from
object
.New-style classes have some advanced features lacking in old-style classes, such as
super
, the new C3 mro, some magical methods, etc.这是一个非常实用的真/假区别。 以下代码的两个版本之间的唯一区别是,在第二个版本中,Person 继承自object。 除此之外,这两个版本是相同的,但结果不同:
旧式类
新式类
Here's a very practical, true/false difference. The only difference between the two versions of the following code is that in the second version Person inherits from object. Other than that, the two versions are identical, but with different results:
Old-style classes
New-style classes
新式类继承自
object
,并且在 Python 2.2 及以上版本中必须这样编写(即class Classname(object):
而不是class Classname:)。 核心变化是统一类型和类,这样做的好处是它允许您从内置类型继承。
阅读 descrintro 了解更多详细信息。
New-style classes inherit from
object
and must be written as such in Python 2.2 onwards (i.e.class Classname(object):
instead ofclass Classname:
). The core change is to unify types and classes, and the nice side-effect of this is that it allows you to inherit from built-in types.Read descrintro for more details.
新样式类可以使用 super(Foo, self) ,其中 Foo 是类,self 是实例。
在 Python 3.x 中,您可以简单地在类中使用 super() ,而不需要任何参数。
New style classes may use
super(Foo, self)
whereFoo
is a class andself
is the instance.And in Python 3.x you can simply use
super()
inside a class without any parameters.