Enhanced dir() function

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jul 1 06:29:35 CEST 2011

The dir() function is designed for interactive use, inspecting objects for
the names of attributes and methods.

Here is an enhanced version that allows you to pass a glob to filter the
names you see:


E.g. instead of this:

>>> dir(str)
['__add__', '__class__', 
 ... snip 60+ other names ... 
 'upper', 'zfill']

you can easily filter the results to show only names that interest you, by
passing a glob:

>>> dir(str, '*split*')
['rsplit', 'split', 'splitlines']

Comments and improvements welcome.


More information about the Python-list mailing list