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

    • <bdo id='ggHE0'></bdo><ul id='ggHE0'></ul>
    1. <small id='ggHE0'></small><noframes id='ggHE0'>

    2. <legend id='ggHE0'><style id='ggHE0'><dir id='ggHE0'><q id='ggHE0'></q></dir></style></legend>
    3. <tfoot id='ggHE0'></tfoot>

      为什么不能在 python 中为对象添加属性?

      时间:2023-09-13

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

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

          • <bdo id='dxOcq'></bdo><ul id='dxOcq'></ul>
            <tfoot id='dxOcq'></tfoot>

                <tbody id='dxOcq'></tbody>
              • <i id='dxOcq'><tr id='dxOcq'><dt id='dxOcq'><q id='dxOcq'><span id='dxOcq'><b id='dxOcq'><form id='dxOcq'><ins id='dxOcq'></ins><ul id='dxOcq'></ul><sub id='dxOcq'></sub></form><legend id='dxOcq'></legend><bdo id='dxOcq'><pre id='dxOcq'><center id='dxOcq'></center></pre></bdo></b><th id='dxOcq'></th></span></q></dt></tr></i><div id='dxOcq'><tfoot id='dxOcq'></tfoot><dl id='dxOcq'><fieldset id='dxOcq'></fieldset></dl></div>
                本文介绍了为什么不能在 python 中为对象添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                (用 Python shell 编写)

                (Written in Python shell)

                >>> o = object()
                >>> o.test = 1
                
                Traceback (most recent call last):
                  File "<pyshell#45>", line 1, in <module>
                    o.test = 1
                AttributeError: 'object' object has no attribute 'test'
                >>> class test1:
                    pass
                
                >>> t = test1()
                >>> t.test
                
                Traceback (most recent call last):
                  File "<pyshell#50>", line 1, in <module>
                    t.test
                AttributeError: test1 instance has no attribute 'test'
                >>> t.test = 1
                >>> t.test
                1
                >>> class test2(object):
                    pass
                
                >>> t = test2()
                >>> t.test = 1
                >>> t.test
                1
                >>> 
                

                为什么对象不允许你给它添加属性?

                Why doesn't object allow you to add attributes to it?

                推荐答案

                注意 object 实例没有 __dict__ 属性:

                Notice that an object instance has no __dict__ attribute:

                >>> dir(object())
                ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
                

                在派生类中说明此行为的示例:

                An example to illustrate this behavior in a derived class:

                >>> class Foo(object):
                ...     __slots__ = {}
                ...
                >>> f = Foo()
                >>> f.bar = 42
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                AttributeError: 'Foo' object has no attribute 'bar'
                

                引用 slots:

                Quoting from the docs on slots:

                [...] __slots__ 声明采用一系列实例变量,并在每个实例中保留足够的空间来保存每个变量的值.节省空间是因为 __dict__ 不是为每个实例创建的.

                [...] The __slots__ declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because __dict__ is not created for each instance.

                从评论中回答 ThomasH,OP 的测试类是一个旧式"类.试试:

                To answer ThomasH from the comments, OP's test class is an "old-style" class. Try:

                >>> class test: pass
                ...
                >>> getattr(test(), '__dict__')
                {}
                >>> getattr(object(), '__dict__')
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                AttributeError: 'object' object has no attribute '__dict__'
                

                您会注意到有一个 __dict__ 实例.对象类可能没有定义 __slots__,但结果是一样的:缺少 __dict__,这是阻止动态分配属性的原因.我已经重新组织了我的答案以使其更清晰(将第二段移到顶部).

                and you'll notice there is a __dict__ instance. The object class may not have a __slots__ defined, but the result is the same: lack of a __dict__, which is what prevents dynamic assignment of an attribute. I've reorganized my answer to make this clearer (move the second paragraph to the top).

                这篇关于为什么不能在 python 中为对象添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Python字符串属性 下一篇:在python中从字典中设置属性

                相关文章

                最新文章

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

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