“对象没有 __dict__,因此您不能将任意属性分配给对象类的实例." [英] "object does not have a __dict__, so you can’t assign arbitrary attributes to an instance of the object class."

查看:45
本文介绍了“对象没有 __dict__,因此您不能将任意属性分配给对象类的实例."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 https://docs.python.org/3.3/library/函数.html#object

<块引用>

object 没有 __dict__,因此您不能将任意属性分配给 object 类的实例.

为什么object 没有 __dict__"?对我有用

<预><代码>>>>object.__dict__mappingproxy({'__repr__': , '__hash__': <slot wrapper '__hash__' of 'object' objects>, '__subclasshook__': <method '__subclasshook__' of'object'对象>, '__ne__': <'object'对象的slot wrapper '__ne__'>, '__format__': <'object'对象的方法'__format__'>, '__new__': <built-in0xa3dc20 处的 object 类型的方法 __new__>, '__doc__': '最基本的类型', '__class__': <'object' 对象的属性 '__class__'>, '__dir__': , '__delattr__': <slot wrapper '__delattr__' of 'object' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>, '__le__': <slot wrapper '__le__'object'对象的','__init__':<'object'对象的slot wrapper'__init__','__gt__':'object'objects的slot wrapper'__gt__','__ge__':<'object'对象的槽包装器'__ge__','__eq__':<槽包装器'__eq'object' 对象的 __'、'__reduce__':<'object' 对象的方法 '__reduce__'、'__lt__':<'object' 对象的槽包装器 '__lt__'、'__str__':<'object'对象的槽包装器'__str__','__reduce_ex__':<'object'对象的方法'__reduce_ex__','__sizeof__':'object'对象的方法'__sizeof__','__setattr__':<'object' 对象的槽包装器 '__setattr__'>})

为什么您不能为 object 类的实例分配任意属性"?

解决方案

你混淆了 type 上的 __dict__ 和实例上的属性.object() instances 没有 __dict__ 属性:

 >>>object().__dict__回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>AttributeError: 'object' 对象没有属性 '__dict__'

请注意,自定义 Python 类实例的 __dict__ 属性是一个 描述符;实例本身没有属性,它是提供它的类(因此返回 type(instance).__dict__['__dict__'].__get__(instance)).object.__dict__ 可能存在,但 object.__dict__['__dict__'] 不存在.

object() 不支持实例属性,因为它是所有自定义 Python 类的基础,必须 支持在定义插槽时没有 __dict__ 属性.

From https://docs.python.org/3.3/library/functions.html#object

object does not have a __dict__, so you can’t assign arbitrary attributes to an instance of the object class.

Why "object does not have a __dict__"? It works for me

>>> object.__dict__
mappingproxy({'__repr__': <slot wrapper '__repr__' of 'object' objects>, '__hash__': <slot wrapper '__hash__' of 'object' objects>, '__subclasshook__': <method '__subclasshook__' of 'object' objects>, '__ne__': <slot wrapper '__ne__' of 'object' objects>, '__format__': <method '__format__' of 'object' objects>, '__new__': <built-in method __new__ of type object at 0xa3dc20>, '__doc__': 'The most base type', '__class__': <attribute '__class__' of 'object' objects>, '__dir__': <method '__dir__' of 'object' objects>, '__delattr__': <slot wrapper '__delattr__' of 'object' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>, '__le__': <slot wrapper '__le__' of 'object' objects>, '__init__': <slot wrapper '__init__' of 'object' objects>, '__gt__': <slot wrapper '__gt__' of 'object' objects>, '__ge__': <slot wrapper '__ge__' of 'object' objects>, '__eq__': <slot wrapper '__eq__' of 'object' objects>, '__reduce__': <method '__reduce__' of 'object' objects>, '__lt__': <slot wrapper '__lt__' of 'object' objects>, '__str__': <slot wrapper '__str__' of 'object' objects>, '__reduce_ex__': <method '__reduce_ex__' of 'object' objects>, '__sizeof__': <method '__sizeof__' of 'object' objects>, '__setattr__': <slot wrapper '__setattr__' of 'object' objects>})

Why "you can’t assign arbitrary attributes to an instance of the object class"?

解决方案

You are confusing the __dict__ on the type with the attribute on instances. object() instances do not have a __dict__ attribute:

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

Note that the __dict__ attribute of custom Python class instances is a descriptor; the instance itself doesn't have the attribute, it is the class that provides it (so type(instance).__dict__['__dict__'].__get__(instance) is returned). object.__dict__ may exist, but object.__dict__['__dict__'] does not.

object() doesn't support instance attributes because it is the base for all custom Python classes, which must support not having a __dict__ attribute when defining slots instead.

这篇关于“对象没有 __dict__,因此您不能将任意属性分配给对象类的实例."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆