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

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

      <legend id='oSgbf'><style id='oSgbf'><dir id='oSgbf'><q id='oSgbf'></q></dir></style></legend>

      <tfoot id='oSgbf'></tfoot>

      1. 类属性和实例属性有什么区别?

        时间:2023-09-13
          <bdo id='hKzf6'></bdo><ul id='hKzf6'></ul>
        • <tfoot id='hKzf6'></tfoot>

            <tbody id='hKzf6'></tbody>

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

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

                  本文介绍了类属性和实例属性有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  以下之间是否有任何有意义的区别:

                  Is there any meaningful distinction between:

                  class A(object):
                      foo = 5   # some default value
                  

                  对比

                  class B(object):
                      def __init__(self, foo=5):
                          self.foo = foo
                  

                  如果您要创建大量实例,这两种样式在性能或空间要求上是否有任何差异?看代码的时候,有没有觉得这两种风格的含义有很大的不同?

                  If you're creating a lot of instances, is there any difference in performance or space requirements for the two styles? When you read the code, do you consider the meaning of the two styles to be significantly different?

                  推荐答案

                  存在显着的语义差异(超出性能考虑):

                  There is a significant semantic difference (beyond performance considerations):

                  • 在实例上定义属性时(这是我们通常所做的),可以引用多个对象.每个人都有一个完全独立的该属性版本.
                  • 当在类上定义属性时,只有一个底层对象被引用,所以如果对该类的不同实例的操作都尝试设置/(追加/扩展/插入/等.) 属性,然后:
                    • 如果属性是内置类型(如int、float、boolean、string),对一个对象的操作将覆盖(破坏)该值
                    • 如果属性是可变类型(如列表或字典),我们会得到不必要的泄漏.
                    • when the attribute is defined on the instance (which is what we usually do), there can be multiple objects referred to. Each gets a totally separate version of that attribute.
                    • when the attribute is defined on the class, there is only one underlying object referred to, so if operations on different instances of that class both attempt to set/(append/extend/insert/etc.) the attribute, then:
                      • if the attribute is a builtin type (like int, float, boolean, string), operations on one object will overwrite (clobber) the value
                      • if the attribute is a mutable type (like a list or a dict), we will get unwanted leakage.

                      例如:

                      >>> class A: foo = []
                      >>> a, b = A(), A()
                      >>> a.foo.append(5)
                      >>> b.foo
                      [5]
                      >>> class A:
                      ...  def __init__(self): self.foo = []
                      >>> a, b = A(), A()
                      >>> a.foo.append(5)
                      >>> b.foo    
                      []
                      

                      这篇关于类属性和实例属性有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:pip install dryscrape 失败并出现“错误:[Errno 2] 没有 下一篇:如何访问与该属性名称对应的给定字符串的对象

                  相关文章

                  最新文章

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

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

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

                  <tfoot id='dHVDW'></tfoot>

                    <legend id='dHVDW'><style id='dHVDW'><dir id='dHVDW'><q id='dHVDW'></q></dir></style></legend>