相关疑难解决方法(0)

Python 如何绕过正常的属性查找来查找`__dict__`?

我知道__dict__ inobj.__dict__是 的描述符属性type(obj),因此查找obj.__dict__是type(obj).__dict__['__dict__'].__get__(obj)。

来自/sf/answers/3260320661/

很容易说它__dict__必须是一个描述符,因为__dict__将它实现为条目将需要您先找到 , __dict__然后才能找到__dict__,但是在查找其他属性时,Python 已经绕过了正常的属性查找来查找__dict__,因此这并不像它最初听起来。如果描述符被替换为 each 中的一个'__dict__'键__dict__,__dict__仍然可以找到。

“Python 已经绕过正常的属性查找来查找__dict__”如何?“正常属性查找”是什么意思?

根据链接中引用的上下文,我不认为作者在撰写该内容时提到了查找obj.__dict__是type(obj).__dict__['__dict__'].__get__(obj).

python attributes descriptor python-3.x python-internals

0
推荐指数
1
解决办法
316
查看次数

"对象没有__dict__,因此您无法将任意属性分配给对象类的实例."

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

object没有__dict__,所以你不能将任意属性分配给object类的实例.

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

>>> 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' …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

-3
推荐指数
1
解决办法
1249
查看次数