[Python-Dev] introducing __dir__?
tomer filiba
tomerfiliba at gmail.com
Thu Jul 6 19:22:51 CEST 2006
i'd guess this has been brought up before, but i'm not aware of it.
anyway --
by what i've gathered, today's dir() "collects" the attributes of the
object from __dict__, __methods__, __members__, and ultimately
__class__. also, if i understood correctly, __methods__ and
__members__ are deprecated. moreover, they are sequence objects,
rather than callbale functions. that means my code can't
my suggestion is simple -- replace this mechanism with a __dir__ -
a special method that returns the list of attributes of the object.
rationale:
* remove deprecated __methods__, etc.
* symmetry -- just like hex() calls __hex__, etc.
* __methods__ and __members__ are lists rather than callable
objects, which means they cannot be updated on-demand
and the reason for bringing this up in the first place --
allowing proxy objects to show the attributes of the internal object.
for example, in rpyc, i had to override __builtin__.dir to achieve
this functionallity, which is of course not the optimal sollution.
implementation:
very straight forward and similar to the rest of the special methods:
* dir() - returns the current scope
* dir(obj) - retuns obj.__dir__(); if the object does not provide __dir__,
uses the current mechanism as a fallback, or something similar
the ultimate object type would grow a default __dir__ method that
uses the same modus operandi as today's dir, which deriving
classes could override to provide custom listings.
therefore, i don't expect any backward-compatibility issues.
aye or nay?
-tomer
More information about the Python-Dev
mailing list