为什么Python类继承对象? [英] Why do Python classes inherit object?
问题描述
是否有任何理由要使类声明从 object
继承?
Is there any reason for a class declaration to inherit from object
?
我刚刚找到了执行此操作的代码,但找不到很好的理由.
I just found some code that does this and I can't find a good reason why.
class MyClass(object):
# class code follows...
推荐答案
是否有任何理由要使类声明从
object
继承?
在Python 3中,除了Python 2和3之间的兼容性外,没有理由.在Python 2中,很多原因.
In Python 3, apart from compatibility between Python 2 and 3, no reason. In Python 2, many reasons.
在Python 2.x(从2.2开始)中,有两种类型的类,取决于 object
作为基类的存在与否:
In Python 2.x (from 2.2 onwards) there's two styles of classes depending on the presence or absence of object
as a base-class:
-
经典"样式类:它们没有
object
作为基类:
"classic" style classes: they don't have
object
as a base class:
>>> class ClassicSpam: # no base class
... pass
>>> ClassicSpam.__bases__
()
"new" style classes: they have, directly or indirectly (e.g inherit from a built-in type), object
as a base class:
>>> class NewSpam(object): # directly inherit from object
... pass
>>> NewSpam.__bases__
(<type 'object'>,)
>>> class IntSpam(int): # indirectly inherit from object...
... pass
>>> IntSpam.__bases__
(<type 'int'>,)
>>> IntSpam.__bases__[0].__bases__ # ... because int inherits from object
(<type 'object'>,)
毫无疑问,当编写一个类时,您将总是想要参加新式的类.这样做的好处很多,列举其中一些:
Without a doubt, when writing a class you'll always want to go for new-style classes. The perks of doing so are numerous, to list some of them:
-
对描述符的支持.具体来说,使用描述符可以实现以下构造:
Support for descriptors. Specifically, the following constructs are made possible with descriptors:
-
classmethod
:接收该类作为隐式参数而不是实例. -
staticmethod
:不会收到隐式参数self
作为第一个参数. 具有 - 属性:创建用于管理属性的获取,设置和删除的功能.
-
__ slots __
:节省内存消耗类,还可以更快地访问属性.当然,它会施加限制.
属性
的classmethod
: A method that receives the class as an implicit argument instead of the instance.staticmethod
: A method that does not receive the implicit argumentself
as a first argument.- properties with
property
: Create functions for managing the getting, setting and deleting of an attribute. __slots__
: Saves memory consumptions of a class and also results in faster attribute access. Of course, it does impose limitations.
__ new __
静态方法:让您自定义如何创建新的类实例.
The __new__
static method: lets you customize how new class instances are created.
方法解析顺序(MRO):按什么顺序尝试解析要调用的方法时,将搜索类的基类.
Method resolution order (MRO): in what order the base classes of a class will be searched when trying to resolve which method to call.
与MRO相关的 super
调用.另请参阅 super()
被认为是超级.
Related to MRO, super
calls. Also see, super()
considered super.
If you don't inherit from object
, forget these. A more exhaustive description of the previous bullet points along with other perks of "new" style classes can be found here.
新型类的缺点之一是类本身对内存的要求更高.不过,除非您要创建许多类对象,否则我会怀疑这将是一个问题,而且这是一个消极的消极情绪.
One of the downsides of new-style classes is that the class itself is more memory demanding. Unless you're creating many class objects, though, I doubt this would be an issue and it's a negative sinking in a sea of positives.
在Python 3中,事情得到了简化.仅存在新样式的类(统称为类),因此添加 object
的唯一区别是要求您再输入8个字符.这个:
In Python 3, things are simplified. Only new-style classes exist (referred to plainly as classes) so, the only difference in adding object
is requiring you to type in 8 more characters. This:
class ClassicSpam:
pass
与此完全等效(除了它们的名称:-):
is completely equivalent (apart from their name :-) to this:
class NewSpam(object):
pass
并为此:
class Spam():
pass
所有人的 __ bases __
中都有 object
.
>>> [object in cls.__bases__ for cls in {Spam, NewSpam, ClassicSpam}]
[True, True, True]
那你该怎么办?
在Python 2中: 总是从 object
显式继承.享受津贴.
So, what should you do?
In Python 2: always inherit from object
explicitly. Get the perks.
在Python 3中:如果要编写试图与Python无关的代码,则继承自 object
,也就是说,它需要在Python 2和Python中都可以工作3.否则不要这么做,因为Python会在幕后为您插入它,所以这实际上没有什么区别.
In Python 3: inherit from object
if you are writing code that tries to be Python agnostic, that is, it needs to work both in Python 2 and in Python 3. Otherwise don't, it really makes no difference since Python inserts it for you behind the scenes.
这篇关于为什么Python类继承对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!