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

  • <tfoot id='kmIBY'></tfoot>

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

    1. <small id='kmIBY'></small><noframes id='kmIBY'>

      1. “私人"名称修改和实例与类属性

        时间:2023-09-13
      2. <i id='gvydb'><tr id='gvydb'><dt id='gvydb'><q id='gvydb'><span id='gvydb'><b id='gvydb'><form id='gvydb'><ins id='gvydb'></ins><ul id='gvydb'></ul><sub id='gvydb'></sub></form><legend id='gvydb'></legend><bdo id='gvydb'><pre id='gvydb'><center id='gvydb'></center></pre></bdo></b><th id='gvydb'></th></span></q></dt></tr></i><div id='gvydb'><tfoot id='gvydb'></tfoot><dl id='gvydb'><fieldset id='gvydb'></fieldset></dl></div>
          <legend id='gvydb'><style id='gvydb'><dir id='gvydb'><q id='gvydb'></q></dir></style></legend>
              <tbody id='gvydb'></tbody>

              <tfoot id='gvydb'></tfoot>
                <bdo id='gvydb'></bdo><ul id='gvydb'></ul>

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

                  本文介绍了“私人"名称修改和实例与类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在编写一个需要访问私有变量的装饰器并发现了这种差异.谁能解释一下?

                  I was writing a decorator that needs to access private variables and found this discrepancy. Can anyone explain this?

                  (Python 2.5)

                  (Python 2.5)

                  命名修改对类中定义的属性按预期工作:

                  Naming mangling works as expected for attributes defined in the class:

                  >>> class Tester(object):
                  ...    __foo = "hi"
                  
                  >>> t = Tester()
                  >>> t._Tester__foo
                  'hi'
                  

                  实例属性不起作用(这是我们应该做的正确的方式?)

                  Instance attributes do not work (and this is the way we are supposed to do it right?)

                  >>> class Tester(object):
                  ...     def __init__(self):
                  ...         self.__foo = "hi"
                  
                  >>> t = Tester()
                  >>> t._Tester__foo
                  AttributeError: 'Tester' object has no attribute '_Tester__foo'
                  

                  附:类属性"是正确的词吗?它们不是静态的,但如果您将其中之一设为列表或其他可变类型,则它是共享的...

                  P.S. Is "class attribute" the right word for these? They aren't static, but if you make one of those a list, or some other mutable type, it is shared...

                  更新

                  事实上,第二个例子也很好用.这是硬件问题(重启有帮助).

                  In fact, second example works fine, too. It was a hardware issue (restart helped).

                  推荐答案

                  这其实是正确的.

                  名称修改发生在类创建时;任何引用重整名称的函数也会被调整.

                  Name mangling takes place at class creation time; any functions that refer to mangled names are adjusted as well.

                  我无法重现您的示例,至少不能在 Mac 上的 Python 版本 2.4、2.5、2.6、3.1 和 3.2 中重现:

                  I cannot reproduce your example, at least not in Python versions 2.4, 2.5, 2.6, 3.1 and 3.2 on the Mac:

                  >>> class Tester(object):
                  ...     def __init__(self):
                  ...         self.__foo = "hi"
                  ... 
                  >>> Tester()._Tester__foo
                  'hi'
                  >>> Tester().__foo
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  AttributeError: 'Tester' object has no attribute '__foo'
                  

                  如果你反汇编函数字节码,你可以看到名字也被破坏了:

                  If you disassemble the function bytecode you can see the name has been mangled as well:

                  >>> import dis
                  >>> dis.dis(Tester.__init__)
                    3           0 LOAD_CONST               1 ('hi')
                                3 LOAD_FAST                0 (self)
                                6 STORE_ATTR               1 (_Tester__foo)
                                9 LOAD_CONST               0 (None)
                               12 RETURN_VALUE        
                  

                  我检查了 编译器源代码 和 所有名称都通过 mangler 运行,这是一条至少从 2002 年以来一直保持不变的代码路径.

                  I've checked the compiler source and all names are run through the mangler, a code path that has remained the same since 2002 at least.

                  是的,类属性和实例属性是正确的术语.类属性始终是共享的,但是将实例上的属性分配给 to 会分配给该实例.改变列表或其他可变对象与属性赋值不同.

                  And yes, class attributes and instance attributes are the correct terms. Class attributes are always shared, but assignment to an attribute on an instance assigns to the instance. Mutating a list or other mutable objects is not the same as attribute assignment.

                  这篇关于“私人"名称修改和实例与类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Django 模型与Python 类属性 下一篇:为什么在 2012 年 python 中的 pandas 合并比 R 中的

                  相关文章

                  最新文章

                • <legend id='qhdj6'><style id='qhdj6'><dir id='qhdj6'><q id='qhdj6'></q></dir></style></legend>

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

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

                    <tfoot id='qhdj6'></tfoot>

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