1

In Python, we are always told that dir lists any attribute of its questioned object. But why isn't there __dict__ in dir("an imported module")?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
AAG
  • 29
  • 4
  • 2
    Why would you expect that? – toti08 Aug 06 '18 at 08:48
  • 2
    I think you're saying that you imported a module, and `dir(modulename)` doesn't include `__dict__`, but `modulename.__dict__` does seem to exist. It might make your question clearer if that was stated in your question instead of just being implicit. – khelwood Aug 06 '18 at 08:50
  • Check out https://stackoverflow.com/questions/14361256/whats-the-biggest-difference-between-dir-and-dict-in-python and look at my answer for some more information. – Adi219 Aug 06 '18 at 09:00
  • @toti08, I think it would be great, if there was a tool that you could list quickly and easily all attributes of an object. I was thinking dir() or something has got this role. – AAG Aug 06 '18 at 11:02
  • @khelwood, you are right. your description is explicit and stated clearly the case. – AAG Aug 06 '18 at 11:07
  • Feel free to [edit] your question to make it clearer. – khelwood Aug 06 '18 at 11:30

3 Answers3

1

You cannot rely on dir([object]) returning accurate results. As stated 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__().

So try looking into the object you are trying to get the information from and look for the attributes yourself.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Jonathan R
  • 3,652
  • 3
  • 22
  • 40
0

The answer is in python documentation for the dir function:

Note: Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.

__dict__ is a special attribute that is part of that exception.

nosklo
  • 217,122
  • 57
  • 293
  • 297
0

dir() doesn't just look up an object's __dict__ (which sometimes doesn't even exist), it will use the object's heritage (its class or type, and any superclasses, or parents, of that class or type) to give you a complete picture of all available attributes.

An instance __dict__ is just the 'local' set of attributes on that instance, and does not contain every attribute available on the instance. Instead, you need to look at the class and the class's inheritance tree too.

For more information, check out the main differences between dir() and __dict__.

Adi219
  • 4,712
  • 2
  • 20
  • 43