Enhanced dir() function

rantingrick rantingrick at gmail.com
Tue Jul 12 23:46:44 CEST 2011

On Jul 1, 12:20 pm, Tim Chase <python.l... at tim.thechases.com> wrote:

> If it came in as an effortless (i.e. O(1) where I do it once and
> never again; not an O(n) where n=the number of times I invoke
> Python) default replacement for dir(), I'd reach for it a lot
> more readily.  I seem to recall there's some environment-var or
> magic file-name that gets sourced on every startup.
> I use the list-comp version on a regular basis:

I strongly agree with this statement because i prefer the LC myself.
HOWEVER i've always lamented the verbosity of dir().
 Case in Point
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__',
'__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__reversed__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__',
'__subclasshook__', 'append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']

Do we really need to see all the built in methods EVERY time? I don't,
i've had them memorized for years. HOWEVER i do understand the fact
that n00bs need to see them every time. So why should old hats need to
type this every time...

>>> [x for x in dir([]) if not x.startswith('_')]
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
'reverse', 'sort']

Because we have plenty of room for args in this function...

>>> dir(verbose=False)
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
'reverse', 'sort']

Ahhh, i love it when a plan comes together!

More information about the Python-list mailing list