gpt4 book ai didi

python - 为什么 dir ("an imported module"中没有 __dict__ 作为属性?

转载 作者:行者123 更新时间:2023-12-01 09:10:26 24 4
gpt4 key购买 nike

在Python中,我们总是被告知dir列出了其所询问对象的任何属性。但为什么 dir("an imported module") 中没有 __dict__ 呢?

最佳答案

您不能依赖 dir([object]) 返回准确的结果。如前所述here :

If the object has a method named __dir__(), this method will be called and must return the list of attributes. This allows objects that implement a custom __getattr__() or __getattribute__() function to customize the way dir() reports their attributes.

If the object does not provide __dir__(), the function tries its best to gather information from the object’s __dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattr__().

因此,请尝试查看您尝试从中获取信息的对象并自行查找属性。

关于python - 为什么 dir ("an imported module"中没有 __dict__ 作为属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51703708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com