给内置对象赋值属性

发布于 2024-11-02 09:06:10 字数 411 浏览 4 评论 0原文

这可行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北凤男飞 2024-11-09 09:06:10

至于背后的理由,BDFL本人的话:

故意禁止这样做是为了防止意外的致命变化
内置类型(对于你从未想过的部分代码来说是致命的)
的)。此外,这样做是为了防止更改影响不同的
由于内置类型,解释器驻留在地址空间中
(与用户定义的类不同)在所有此类之间共享
口译员。

As for the rationale behind this, the words of the BDFL himself:

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.

呢古 2024-11-09 09:06:10

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__']

重要的区别是object没有__dict__属性。

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

更详细的解释:

  • 无法设置对象类的属性
  • <一href="https://stackoverflow.com/questions/1285269/why-cant-you-add-attributes-to-object-in-python/1285287#1285287">为什么不能在Python中向对象添加属性?

Python stores attributes in a dict. You can add attributes to MyClass, see it has a __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.

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

More detailed explanations:

狼亦尘 2024-11-09 09:06:10

对于用户定义的类,在对象上设置属性实际上是修改属性字典,因此您可以随时添加新条目。 (对象的__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

傲性难收 2024-11-09 09:06:10
>>> type(object)
type 'type'
>>> type(MyClass)
type 'classobj'

这里重要的区别是 MyClass 是用户定义的类对象。您可以在其中修改您的课程。

然而,object() 是一个 __builtin__ 类对象。

当您从基类 object 以及 __builtin__ 继承时,您只能修改您定义的新 MyClass

>>> 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.

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 new MyClass that you defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文