[Python-ideas] dir with a glob?

Ben Finney ben+python at benfinney.id.au
Fri Jul 1 02:27:52 CEST 2011


Georg Brandl <g.brandl at gmx.net> writes:

> On 30.06.2011 15:09, Mathias Panzenböck wrote:
> > Then one could use it in a filter expression:
> > 
> > 	filter(re.compile("^i"), dir(sp.fft))
>
> What's wrong with
>
>    filter(re.compile("^i").search, dir(sp.fft))

(I'm confused over this example, since there are no attributes of
‘scipy.fft’ that start with ‘i’. I'll switch to an example where we're
looking for its attributes that start with ‘f’.)

The globs people have shown are AFAICT easily done by the existing
string methods, and don't need regex. So why not:

>>> import scipy
>>> [a for a in dir(scipy.fft) if a.startswith("f")]
['func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']

-- 
 \        “I don't accept the currently fashionable assertion that any |
  `\       view is automatically as worthy of respect as any equal and |
_o__)                                   opposite view.” —Douglas Adams |
Ben Finney




More information about the Python-ideas mailing list