给内置对象赋值属性
这可行:
class MyClass(object):
pass
someinstance = MyClass()
someinstance.myattribute = 42
print someinstance.myattribute
>>> 42
但这不行:
someinstance = object()
someinstance.myattribute = 42
>>> AttributeError: 'object' object has no attribute 'myattribute'
为什么?我有一种感觉,这与对象是内置类有关,但我发现这并不令人满意,因为我在 MyClass 的声明中没有进行任何更改。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
至于背后的理由,BDFL本人的话:
As for the rationale behind this, the words of the BDFL himself:
Python 将属性存储在字典中。你可以向
MyClass
添加属性,看到它有一个__dict__
:重要的区别是
object
没有__dict__
属性。更详细的解释:
Python stores attributes in a dict. You can add attributes to
MyClass
, see it has a__dict__
:The important difference is that
object
has no__dict__
attribute.More detailed explanations:
对于用户定义的类,在对象上设置属性实际上是修改属性字典,因此您可以随时添加新条目。 (对象的
__dict__
)内置类型的属性以不同的方式实现,不是作为通用字典,而是直接作为底层实现的内存布局。由于字典不存在,并且类无法在运行时更改,因此无法向内置对象添加新的属性值。
请参阅:
http://webcache.googleusercontent.com/search?q=cache:z4to2IGbKDUJ:www.velocityreviews.com/forums/t593269-cant-set-attributes-of- built-in-extension-type.html+setattr+built+in+class&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a&source=www.google.com
http://mail.python.org/pipermail/python-dev /2008-2月/077180.html
http://mail.python.org/pipermail/python-list/2010 -四月/1240731.html
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__
)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+built+in+class&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a&source=www.google.com
http://mail.python.org/pipermail/python-dev/2008-February/077180.html
http://mail.python.org/pipermail/python-list/2010-April/1240731.html
这里重要的区别是
MyClass
是用户定义的类对象。您可以在其中修改您的课程。然而,
object()
是一个__builtin__
类对象。当您从基类 object 以及 __builtin__ 继承时,您只能修改您定义的新
MyClass
。Here the important difference is
MyClass
is a user defined class object. Where you can modify your class.object()
however is a__builtin__
class object.When you inherit from object which is your base class as well as
__builtin__
, you can modify only your newMyClass
that you defined.