Regex anomaly

Bryan Olson fakeaddress at nowhere.org
Thu Jan 5 01:32:35 EST 2006


Roy Smith wrote:
> LOL, and you'll be LOL too when you see the problem :-)
> 
> You can't give the re.I flag to reCompiled.match().  You have to give
> it to re.compile().  The second argument to reCompiled.match() is the
> position where to start searching.  I'm guessing re.I is defined as 2,
> which explains the match you got.
> 
> This is actually one of those places where duck typing let us down.
> If we had type bondage, re.I would be an instance of RegExFlags, and
> reCompiled.match() would have thrown a TypeError when the second
> argument wasn't an integer.  I'm not saying type bondage is inherently
> better than duck typing, just that it has its benefits at times.


Even with duck-typing, we could cut our users a break. Making
our flags instances of a distinct class doesn't actually require
type bondage.

We could define the __or__ method for RegExFlags, but really,
or-ing together integer flags is old habit from low-level
languages. Really we should pass a set of flags.


-- 
--Bryan



More information about the Python-list mailing list