473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

__dir__

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
2 2942
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 430 open ( -4) / 3447 closed (+17) / 3877 total (+13) Bugs : 922 open ( -7) / 6316 closed (+31) / 7238 total (+24) RFE : 245 open...
7
by: JonathanB | last post by:
Ok, I know there has to be a way to do this, but my google-fu fails me (once more). I have a class with instance variables (or should they be called attributes, I'm newish to programming and get...
3
by: Tomohiro Kusumi | last post by:
Diez, Thanks, you're right. Delegated attributes are not in the dir() result. '+' 0 {} Tomohiro Kusumi
5
by: Paul McGuire | last post by:
I've just uploaded to SourceForge and PyPI the latest update to pyparsing, version 1.5.1. It has been a couple of months since 1.5.0 was released, and a number of bug-fixes and enhancements have...
11
by: Jivanmukta | last post by:
Hello, I am learning PHP5. I need to obtain my website's root directory in .php code. I tried the code: echo __DIR__, $_SERVER; but it displays: __DIR__/var/www/html although my_website is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.