By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

__dir__

P: n/a
Is there some way of affecting the way dir finds the information it presents?

For example, supposing a hypothetical __dir__ magic method:

class collection:
def __init__(self, myname, names):
self.names = names
self.myname = myname
def __dir__(self):
return ['%s(%s)' % (self.myname,n) for n in self.names]

class thingy:
def __init__(self, *names):
self.it = collection('it', names)

thing = thingy('this', 'that', 'theother')

dir(thing)

['__doc__', '__init__', '__module__', 'it(this)', 'it(that)', 'it(theother)']
In other words, I would like some attributes of an instance to inject
some information into the output of dir ... to change the way their
names are perceived by dir.

Any ideas ?
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jacek Generowicz wrote:
Is there some way of affecting the way dir finds the information it
presents?

For example, supposing a hypothetical __dir__ magic method:

class collection:
def __init__(self, myname, names):
self.names = names
self.myname = myname
def __dir__(self):
return ['%s(%s)' % (self.myname,n) for n in self.names]

class thingy:
def __init__(self, *names):
self.it = collection('it', names)

thing = thingy('this', 'that', 'theother')

dir(thing)

['__doc__', '__init__', '__module__', 'it(this)', 'it(that)',
['it(theother)']
In other words, I would like some attributes of an instance to inject
some information into the output of dir ... to change the way their
names are perceived by dir.

Any ideas ?

class C: .... __members__ = ["Is this evil?", "So what"]
.... dir(C) ['__doc__', '__members__', '__module__'] dir(C()) ['Is this evil?', 'So what', '__doc__', '__members__', '__module__']


However, __members__ is marked as deprecated and, even more important, I
don't know what the side effects of the above may be.
Highly unrecommended.

Peter
Jul 18 '05 #2

P: n/a
Peter Otten <__*******@web.de> writes:
However, __members__ is marked as deprecated and, even more important, I
don't know what the side effects of the above may be.
Highly unrecommended.


Great, sounds just perfect for my needs ;-)
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.