[Python-Dev] [NPERS] Re: a feature i'd like to see in python #2: indexing of match objects (original) (raw)

Fredrik Lundh fredrik at pythonware.com
Tue Dec 5 16:51:35 CET 2006


Alastair Houghton wrote:

(The current groups() method doesn't match those expectations, incidentally. I know I've been tripped up in the past because it didn't include the full match as element 0.)

that's because there is no "group 0" in a regular expression; that's just a historical API convenience thing. groups are numbered from 1 and upwards, and "groups()" returns all the actual groups.

What's more, I think it will be confusing for Python newbies because they'll see someone doing

m[3] and assume that m is a list-like object, then complain when things like for match in m: print match

that'll work, of course, which might be confusing for people who think they understand how for-in works but don't ;)

or

m[3:4] fail to do what they expect.

the problem with slicing is that people may 1) expect a slice to return a new object of the same type (which opens up a gigantic can of worms, both on the implementation level and on the wtf-is-this-thing- really level), and 2) expect things like [::-1] to work, which opens up another can of worms. I prefer the "If the implementation is easy to explain, it may be a good idea." design principle over "can of worms" design principle.



More information about the Python-Dev mailing list