Enhanced dir() function

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jul 1 00:29:35 EDT 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:

http://code.activestate.com/recipes/577774-enhancing-dir-with-globs/

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.



-- 
Steven




More information about the Python-list mailing list