Why aren't all the names in the dir (x) directory valid for attribute access?

Why __dict__can't encoder material be used to access an attribute? For example, in my instance, Plone dir(portal)includes index_htmlbut portal.index_htmlthrows an AttributeError. This is also true for the attribute __class__ Products.ZCatalog.Catalog.mybrains. Is there a good reason why dir()you can’t be trusted?

Similar to a module inspect, I see that they use object.__dict__['x']instead of accessing attributes for this reason and because they do not want to run magic getattr.

+3
source share
1 answer

I do not know about Plone, so the following is general.

From the docs dir :

__dir__(), . , __getattr__() __getattribute__(), dir().

, , -

  • __dir__(), ,

  • ( ) , (.. obj.__dict__ type(obj).__dict__, __getattr__ AttributeError

EDIT: __dir__ Python 2.6+, ( ) __methods__ __members__ .

+2

Source: https://habr.com/ru/post/1702325/


All Articles