[Python-ideas] Allow filtered dir built in

Michel Desmoulin desmoulinmichel at gmail.com
Fri Jun 15 04:10:19 EDT 2018



Le 14/06/2018 à 11:27, Steve Barnes a écrit :
> 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.
> 

Fantastic idea. Would this make sense on var() too ? It's not exactly
the same usage context.


More information about the Python-ideas mailing list