在 Python 中对类“对象”进行子类化的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2588628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 01:00:41  来源:igfitidea点击:

What is the purpose of subclassing the class "object" in Python?

pythonobjectdeprecatedfuture-proofnew-style-class

提问by David Wolever

All the Python built-ins are subclasses of objectand I come across many user-defined classes which are too. Why? What is the purpose of the class object? It's just an empty class, right?

所有 Python 内置object函数都是 的子类,我也遇到过许多用户定义的类。为什么?上课的目的是object什么?这只是一个空类,对吧?

回答by David Wolever

In short, it sets free magical ponies.

简而言之,它释放了魔法小马。

In long, Python 2.2 and earlier used "old style classes". They were a particular implementation of classes, and they had a few limitations (for example, you couldn't subclass builtin types). The fix for this was to create a new style of class. But, doing this would involve some backwards-incompatible changes. So, to make sure that code which is written for old style classes will still work, the objectclass was created to act as a superclass for all new-style classes. So, in Python 2.X, class Foo: passwill create an old-style class and class Foo(object): passwill create a new style class.

长期以来,Python 2.2 及更早版本使用“旧式类”。它们是类的特定实现,并且有一些限制(例如,您不能对内置类型进行子类化)。解决这个问题的方法是创建一种新的类样式。但是,这样做会涉及一些向后不兼容的更改。因此,为了确保为旧样式类编写的代码仍然有效,object创建该类作为所有新样式类的超类。因此,在 Python 2.X 中,class Foo: pass将创建一个旧样式类,class Foo(object): pass并将创建一个新样式类。

In longer, see Guido's Unifying types and classes in Python 2.2.

更长的时间,请参阅 Guido 的Unifying types and classes in Python 2.2

And, in general, it's a good idea to get into the habit of making all your classes new-style, because some things (the @propertydecorator is one that comes to mind) won't work with old-style classes.

而且,一般来说,养成将所有类@property都设为新样式的习惯是个好主意,因为有些东西(装饰器是我想到的)不适用于旧样式的类。

回答by ChristopheD

Short answer: subclassing objecteffectively makes it a new-styleclass (note that this is unnecessary since automatic in Python 3.x)

简短回答:子类化object有效地使其成为一种新型类(请注意,这是不必要的,因为在 Python 3.x 中是自动的)

For the difference between new styleclasses and old styleclasses: see this stackoverflow question. For the complete story: see this nice writeup on Python Types and Objects.

有关new style类和old style类之间的区别:请参阅此 stackoverflow 问题。有关完整的故事:请参阅有关 Python 类型和对象的这篇不错的文章

回答by thetaiko

It has to do with the "new-style" of classes. You can read more about it here: http://docs.python.org/tutorial/classes.html#multiple-inheritanceand also here: http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes

它与类的“新式”有关。您可以在此处阅读更多相关信息:http: //docs.python.org/tutorial/classes.html#multiple-inheritance以及此处:http: //docs.python.org/reference/datamodel.html#new-style -and-classic-classes

Using new-style classes will allow you to use "Python's newer, versatile features like __slots__, descriptors, properties, and __getattribute__()."

使用新样式的类将允许您使用“Python 的更新、通用功能,如 __slots__、描述符、属性和 __getattribute__()”。

回答by msw

Right, but it marks the class as a new-styleclass. Newly developed classes should use the objectbase because it costs little and future-proofs your code.

是的,但它将类标记为新型类。新开发的类应该使用object基类,因为它成本低,并且可以使您的代码面向未来。

回答by Mike DeSimone

The short version is that classic classes, which didn't need a superclass, had limitations that couldn't be worked around without breaking a lot of old code. So they created the concept of new-style classes which subclass from object, and now you can do cool things like define properties, and subclassing dictis no longer an exercise in pain and strange bugs.

简短的版本是不需要超类的经典类具有在不破坏大量旧代码的情况下无法解决的限制。因此,他们创造了新的样式类,其从子类的概念object,现在你可以做很酷的事情想定义属性,子类dict不再疼痛和奇怪的错误的练习。

The details are in section 3.3 of the Python docs: New-style and classic classes.

详细信息在 Python 文档的第 3.3 节:New-style and classic classes。

回答by jemfinch

Python 2.2 introduced "new style classes"which had a number of additional features relative to the old style classes which did not subclass object. Subclasses object was the chosen way to indicate that your class should be a new style class, not an old style one.

Python 2.2 引入了“新样式类”,相对于没有子类对象的旧样式类,它具有许多附加功能。Subclasses 对象是选择的方式来表明您的类应该是一个新样式类,而不是旧样式类。