[Python-ideas] dir with a glob?
Peter Otten
__peter__ at web.de
Thu Jun 30 16:54:03 CEST 2011
Ben Finney wrote:
> Sturla Molden <sturla at molden.no> writes:
>
>> dir(object, "foo*")
>
> I ask again: what would you expect (give an example) the output of this
> to be?
If I were to guess:
>>> import __builtin__, fnmatch
>>> def dir(obj, glob=None):
... names = __builtin__.dir(obj)
... if glob is not None:
... names = fnmatch.filter(names, glob)
... return names
...
Example usage: what was the name of the function to remove a directory and
all empty parents again?
>>> import os
>>> dir(os)
[snip list with more than 200 names]
>>> dir(os, "r*d*")
['read', 'readlink', 'removedirs', 'rmdir']
Ah, I think I remember it now...
>> What I am asking is if the need to filter the output from dir is so
>> common that it could warrant a change to Python?
At the moment I'm writing list comprehensions in cases like the above, but
I'd welcome the addition of a glob or regex argument to dir().
> Given that we already have ways to filter a sequence built into the
> language, I doubt the need for a special way to filter the output from
> some particular function.
More information about the Python-ideas
mailing list