This question already has an answer here:
这个问题已经有了答案:
- Can't set attributes of object class 6 answers
- 不能设置对象类6答案的属性
This works:
如此:
class MyClass(object):
pass
someinstance = MyClass()
someinstance.myattribute = 42
print someinstance.myattribute
>>> 42
But this doesn't:
但这并不是:
someinstance = object()
someinstance.myattribute = 42
>>> AttributeError: 'object' object has no attribute 'myattribute'
Why? I've got a feeling, that this is related to object being a built-in class, but I find this unsatisfactory, since I changed nothing in the declaration of MyClass.
为什么?我有一种感觉,这与对象是一个内置的类有关,但是我发现这并不令人满意,因为我在MyClass的声明中没有改变任何东西。
4 个解决方案
#1
7
Python stores attributes in a dict. You can add attributes to MyClass
, see it has a __dict__
:
Python在一个命令中存储属性。您可以向MyClass添加属性,它有一个__dict__:
>>> class MyClass(object):
>>> pass
>>> dir(MyClass)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
The important difference is that object
has no __dict__
attribute.
重要的区别是对象没有__dict__属性。
>>> dir(object)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
More detailed explanations:
更详细的解释:
- Can't set attributes of object class
- 不能设置对象类的属性
- Why can't you add attributes to object in python?
- 为什么不能在python中向对象添加属性?
#2
4
As for the rationale behind this, the words of the BDFL himself:
至于背后的基本原理,BDFL自己说:
This is prohibited intentionally to prevent accidental fatal changes to built-in types (fatal to parts of the code that you never though of). Also, it is done to prevent the changes to affect different interpreters residing in the address space, since built-in types (unlike user-defined classes) are shared between all such interpreters.
这是故意禁止的,以防止对内置类型的意外致命更改(对代码的某些部分是致命的,您从来没有这样做过)。此外,还可以防止更改影响驻留在地址空间中的不同解释器,因为内置类型(不像用户定义的类)在所有这些解释器之间共享。
#3
1
>>> type(object)
type 'type'
>>> type(MyClass)
type 'classobj'
Here the important difference is MyClass
is a user defined class object. Where you can modify your class.
这里重要的区别是MyClass是一个用户定义的类对象。可以修改类的地方。
object()
however is a __builtin__
class object.
然而,object()是一个__builtin__类对象。
When you inherit from object which is your base class as well as __builtin__
, you can modify only your new MyClass
that you defined.
当您从对象(即您的基类和__builtin__)继承时,您只能修改定义的新MyClass。
#4
0
For user-defined classes, setting an attribute on an object is actually modifying an dictionary of attributes, so you can add a new entry at any time. (Object's __dict__
)
对于用户定义的类,在对象上设置属性实际上是修改属性字典,因此您可以随时添加一个新条目。(对象的__dict__)
Attributes on built-in types are implemented differently, not as generic dictionary but directly as the memory layout of the underlying implementation. Because the dictionary does not exist, and the class cannot be changed at runtime, you cannot add new attribute values to built-in objects.
内置类型上的属性实现方式不同,不是作为通用字典,而是直接作为底层实现的内存布局实现。因为字典不存在,并且在运行时不能更改类,所以不能向内置对象添加新的属性值。
Please see:
请参阅:
http://webcache.googleusercontent.com/search?q=cache z4to2IGbKDUJ:www.velocityreviews.com/forums/t593269-cant-set-attributes-of-built-in-extension-type.html + setattr +了+ +级光盘= 3 hl = en&ct = clnk&gl =美国客户= firefox-a&source = www.google.com
http://mail.python.org/pipermail/python-dev/2008-February/077180.html
http://mail.python.org/pipermail/python-dev/2008-February/077180.html
http://mail.python.org/pipermail/python-list/2010-April/1240731.html
http://mail.python.org/pipermail/python-list/2010-April/1240731.html
#1
7
Python stores attributes in a dict. You can add attributes to MyClass
, see it has a __dict__
:
Python在一个命令中存储属性。您可以向MyClass添加属性,它有一个__dict__:
>>> class MyClass(object):
>>> pass
>>> dir(MyClass)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
The important difference is that object
has no __dict__
attribute.
重要的区别是对象没有__dict__属性。
>>> dir(object)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
More detailed explanations:
更详细的解释:
- Can't set attributes of object class
- 不能设置对象类的属性
- Why can't you add attributes to object in python?
- 为什么不能在python中向对象添加属性?
#2
4
As for the rationale behind this, the words of the BDFL himself:
至于背后的基本原理,BDFL自己说:
This is prohibited intentionally to prevent accidental fatal changes to built-in types (fatal to parts of the code that you never though of). Also, it is done to prevent the changes to affect different interpreters residing in the address space, since built-in types (unlike user-defined classes) are shared between all such interpreters.
这是故意禁止的,以防止对内置类型的意外致命更改(对代码的某些部分是致命的,您从来没有这样做过)。此外,还可以防止更改影响驻留在地址空间中的不同解释器,因为内置类型(不像用户定义的类)在所有这些解释器之间共享。
#3
1
>>> type(object)
type 'type'
>>> type(MyClass)
type 'classobj'
Here the important difference is MyClass
is a user defined class object. Where you can modify your class.
这里重要的区别是MyClass是一个用户定义的类对象。可以修改类的地方。
object()
however is a __builtin__
class object.
然而,object()是一个__builtin__类对象。
When you inherit from object which is your base class as well as __builtin__
, you can modify only your new MyClass
that you defined.
当您从对象(即您的基类和__builtin__)继承时,您只能修改定义的新MyClass。
#4
0
For user-defined classes, setting an attribute on an object is actually modifying an dictionary of attributes, so you can add a new entry at any time. (Object's __dict__
)
对于用户定义的类,在对象上设置属性实际上是修改属性字典,因此您可以随时添加一个新条目。(对象的__dict__)
Attributes on built-in types are implemented differently, not as generic dictionary but directly as the memory layout of the underlying implementation. Because the dictionary does not exist, and the class cannot be changed at runtime, you cannot add new attribute values to built-in objects.
内置类型上的属性实现方式不同,不是作为通用字典,而是直接作为底层实现的内存布局实现。因为字典不存在,并且在运行时不能更改类,所以不能向内置对象添加新的属性值。
Please see:
请参阅:
http://webcache.googleusercontent.com/search?q=cache z4to2IGbKDUJ:www.velocityreviews.com/forums/t593269-cant-set-attributes-of-built-in-extension-type.html + setattr +了+ +级光盘= 3 hl = en&ct = clnk&gl =美国客户= firefox-a&source = www.google.com
http://mail.python.org/pipermail/python-dev/2008-February/077180.html
http://mail.python.org/pipermail/python-dev/2008-February/077180.html
http://mail.python.org/pipermail/python-list/2010-April/1240731.html
http://mail.python.org/pipermail/python-list/2010-April/1240731.html