[Python-Dev] dir()/__all__/etc

Moshe Zadka moshez@zadka.site.co.il
Fri, 12 Jan 2001 11:57:32 +0200 (IST)

On Thu, 11 Jan 2001 11:11:23 -0600 (CST), Skip Montanaro <skip@mojam.com> wrote:
> I know Guido has said he doesn't want to fiddle with dir(), but my sense of
> things from the overall discussion of the __exports__ concept tells me that
> when used interactively dir() often presents confusing output for new Python
> users.
> I twiddled CGIHTTPServer to have __all__ and added the following dir()
> function to my PYTHONSTARTUP file:
> def dir(o,showall=0):
>     if not showall and hasattr(o, "__all__"):
>         x = list(o.__all__)
>         x.sort()
>         return x
>     from __builtin__ import dir as d
>     return d(o)
> Compare its output with and without showall set:
>   >>> dir(CGIHTTPServer)
>   ['CGIHTTPRequestHandler', 'test']
>   >>> dir(CGIHTTPServer,1)
>   ['BaseHTTPServer', 'CGIHTTPRequestHandler', 'SimpleHTTPServer', '__all__',
>    '__builtins__', '__doc__', '__file__', '__name__', '__version__',
>    'executable', 'nobody', 'nobody_uid', 'os', 'string', 'sys', 'test',
>    'urllib']
> I haven't demonstrated any great programming prowess with this little
> function, but I rather suspect it may be beyond most brand new users.  If
> Guido can't be convinced to allow dir() to change, how about adding a sample
> PYTHONSTARTUP file to the distribution that contains little bits like this
> and Ping's pydoc.help stuff (assuming it gets into the distro, which I hope
> it does)?

And, while we're at it, the following bit too can be in the PYTHONSTARTUP:

def display(x):
	import __builtin__
	__builtin__._ = None
	if type(x) == type(''):
		print `x`
		print x
	__built__._ = x

import sys
sys.displayhook = display

Moshe Zadka <sig@zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!