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

Skip Montanaro skip@mojam.com (Skip Montanaro)
Thu, 11 Jan 2001 11:11:23 -0600 (CST)

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

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__)
        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',

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)?