[Python-ideas] Allow filtered dir built in

Steve Barnes gadgetsteve at live.co.uk
Thu Jun 14 05:27:05 EDT 2018

Currently when working with interactive sessions using the dir() or 
dir(module) built in is incredibly useful for exploring what 
functionality is available in a module. (Especially the regrettable 
libraries or modules that add really valuable functionality but have no 
or limited docstrings).

However I often find that when a module adds a lot of functions I need 
to filter those entries to be able to find the one that I need, e.g.:

 >>> import mpmath
 >>> dir(mpmath)  # This produces 390+ lines of output but
 >>> for name in dir(mpmath):
...    if 'sin' in name:
...        print(name)  # gives me a mere 13 to consider as candidates

What I would really like to do is:
 >>> dir(mpmath.*sin*)

However, I know that the interpreter will hit problems with one or more 
operators being embedded in the module name.

What I would like to suggest is extending the dir built-in to allow an 
optional filter parameter that takes fnmatch type wild card as an 
optional filter. Then I could use:

 >>> dir(mpmath, "*sin*")

To narrow down the candidates.

Ideally, this could have a recursive variant that would also include 
listing, (and filtering), any sub-packages.

Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

This email has been checked for viruses by AVG.

More information about the Python-ideas mailing list