[Python-ideas] dir with a glob?

Georg Brandl g.brandl at gmx.net
Thu Jun 30 19:27:12 CEST 2011

On 30.06.2011 15:09, Mathias Panzenböck wrote:
> I would rather add a grep method to regular expression objects:
> 	def grep(self,sequence):
> 		for s in sequence:
> 			if self.search(s):
> 				yield s
> And a global function to the re module:
> 	def grep(regex,sequence,flags=0):
> 		return re.compile(regex,flags).grep(sequence)
> Then you could do:
> 	re.grep("^i", dir(sp.fft))
> Yes, it would be more to type but it would be more general.

No you couldn't, since this would return <generator ...> -- an explicit
list() would be required.

> OT: I think it would be good if regular expressions would be callable with this method:
> 	def __call__(self,s):
> 		return self.search(s) is not None
> 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))

?  And for non-interactive use, you almost always have a compiled regex object
already, so that this becomes

   filter(rex.search, seq)

which is not sufficiently harder than


to justify a new regex method.  (Also, people won't be able to remember if
grep() uses match() or search().)


More information about the Python-ideas mailing list