[issue40098] dir() does not return the list of valid attributes for the object

Guido van Rossum report at bugs.python.org
Sun Mar 29 14:02:58 EDT 2020


Guido van Rossum <guido at python.org> added the comment:

I think you're going too far for some of these.

> 1. metaclasses

This is reasonable.

> 2. __mro__

This is also reasonable. (I wonder if that part of the dir() implementation predates __mro__?)  I'm not sure about honoring mro() in the metaclass, but it may be useful.

> 3. __dict__

I think this is probably a feature -- I can't think of a reason to override __dict__ as a property except when implementing some sort of proxy class, and then it's likely that there's a matching overrid of __getattr__.

> 4. __class__

Again, I think this is a feature. For example, PEP 585 overrides __class__:

>>> t = list[int]
>>> type(t)
<class 'types.GenericAlias'>
>>> t.__class__
<class 'type'>

> 4.1 auto-creation of instance dict

This seems an accident of implementation, and if you can avoid it, that's better. (Though what use case of dir() currently suffers from memory overhead due to this?)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40098>
_______________________________________


More information about the Python-bugs-list mailing list