<tfoot id='GUHU2'></tfoot>
<legend id='GUHU2'><style id='GUHU2'><dir id='GUHU2'><q id='GUHU2'></q></dir></style></legend>
    1. <i id='GUHU2'><tr id='GUHU2'><dt id='GUHU2'><q id='GUHU2'><span id='GUHU2'><b id='GUHU2'><form id='GUHU2'><ins id='GUHU2'></ins><ul id='GUHU2'></ul><sub id='GUHU2'></sub></form><legend id='GUHU2'></legend><bdo id='GUHU2'><pre id='GUHU2'><center id='GUHU2'></center></pre></bdo></b><th id='GUHU2'></th></span></q></dt></tr></i><div id='GUHU2'><tfoot id='GUHU2'></tfoot><dl id='GUHU2'><fieldset id='GUHU2'></fieldset></dl></div>

          <bdo id='GUHU2'></bdo><ul id='GUHU2'></ul>

        <small id='GUHU2'></small><noframes id='GUHU2'>

        为什么 Python 类会继承对象?

        时间:2024-04-21

          1. <legend id='lpJxt'><style id='lpJxt'><dir id='lpJxt'><q id='lpJxt'></q></dir></style></legend><tfoot id='lpJxt'></tfoot>

              <tbody id='lpJxt'></tbody>
            • <i id='lpJxt'><tr id='lpJxt'><dt id='lpJxt'><q id='lpJxt'><span id='lpJxt'><b id='lpJxt'><form id='lpJxt'><ins id='lpJxt'></ins><ul id='lpJxt'></ul><sub id='lpJxt'></sub></form><legend id='lpJxt'></legend><bdo id='lpJxt'><pre id='lpJxt'><center id='lpJxt'></center></pre></bdo></b><th id='lpJxt'></th></span></q></dt></tr></i><div id='lpJxt'><tfoot id='lpJxt'></tfoot><dl id='lpJxt'><fieldset id='lpJxt'></fieldset></dl></div>

                • <bdo id='lpJxt'></bdo><ul id='lpJxt'></ul>

                  <small id='lpJxt'></small><noframes id='lpJxt'>

                  本文介绍了为什么 Python 类会继承对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  类声明是否有任何理由从 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:

                  1. 经典"风格类:它们没有 object 作为基类:

                  1. "classic" style classes: they don't have object as a base class:

                  >>> class ClassicSpam:      # no base class
                  ...     pass
                  >>> ClassicSpam.__bases__
                  ()
                  

                • 新"样式类:它们直接或间接拥有(例如从内置类型),object作为基类:

                • "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:

                  1. classmethod:一种方法接收类作为隐式参数而不是实例.
                  2. staticmethod:一种方法不接收隐式参数 self 作为第一个参数.
                  3. 具有 property 的属性:创建用于管理属性的获取、设置和删除的功能.
                  4. __slots__:节省内存消耗一个类,也导致更快的属性访问.当然,它确实施加限制.
                  1. classmethod: A method that receives the class as an implicit argument instead of the instance.
                  2. staticmethod: A method that does not receive the implicit argument self as a first argument.
                  3. properties with property: Create functions for managing the getting, setting and deleting of an attribute.
                  4. __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.

                  如果你不是从 object 继承的,忘记这些.可以在 这里.

                  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 中:object 继承,如果您编写的代码试图与 Python 无关,也就是说,它需要在 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 类会继承对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                • 上一篇:在 Python 中创建一个带有重复键的字典 下一篇:等到页面用 Selenium WebDriver for Python 加载

                  相关文章

                    <small id='485DO'></small><noframes id='485DO'>

                      <bdo id='485DO'></bdo><ul id='485DO'></ul>
                  1. <i id='485DO'><tr id='485DO'><dt id='485DO'><q id='485DO'><span id='485DO'><b id='485DO'><form id='485DO'><ins id='485DO'></ins><ul id='485DO'></ul><sub id='485DO'></sub></form><legend id='485DO'></legend><bdo id='485DO'><pre id='485DO'><center id='485DO'></center></pre></bdo></b><th id='485DO'></th></span></q></dt></tr></i><div id='485DO'><tfoot id='485DO'></tfoot><dl id='485DO'><fieldset id='485DO'></fieldset></dl></div>
                    <legend id='485DO'><style id='485DO'><dir id='485DO'><q id='485DO'></q></dir></style></legend><tfoot id='485DO'></tfoot>