hide object property from dir() function?

Tim Golden mail at timgolden.me.uk
Tue Jan 15 04:05:58 EST 2008


jerryji wrote:
> Sorry for this newbie question, I was puzzled why the existing
> property of an object is not shown in the dir() function output.

The under-development version of Python (2.6) allows for a
__dir__ magic method by which the class implementer can
return whatever he wishes from a dir (). This is to help,
for example, modules like my WMI one which makes heavy use
of __getattr__ magic to proxy across Windows COM attributes.
This, in turn, helps editors and IDEs which can provide
popup lists of attributes etc.

All that said, I don't believe it takes any automatic
account of properties.

TJG

<noddy code example>
class X (object):
   def __init__ (self, a):
     self.a = a

print dir (X (1))

def __dir__ (self):
   return ['x', 'y', 'z']

X.__dir__ = __dir__

print dir (X (2))

</code>




More information about the Python-list mailing list