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

Ben Wing ben at 666.com
Mon Dec 4 03:26:54 CET 2006



Martin v. Löwis wrote:
> Aahz schrieb:
>   
>>>> this one is fairly simple.  if `m' is a match object, i'd like to be
>>>> able to write m[1] instead of m.group(1). (similarly, m[:] should return
>>>> the same as list(m.groups()).) this would remove some of the verbosity
>>>> of regexp code, with probably a net gain in readability; certainly no loss.
>>>>         
>>> Please post a patch to sf.net/projects/python (or its successor).
>>>       
>> Given the list of issues and subsequent discussion so far, I think a PEP
>> will be required.  This needs more documentation than the typical patch.
>>     
>
> I disagree. So far, nobody has spoken against the proposed feature. It's
> really a small addition of a new method to an existing type. Entire
> classes have been added to the standard library without a PEP. People
> can still criticize the patch when its posted (and it's not clear that
> the OP is even willing to produce a patch).
>
>   
i've never worked up a python patch before, but i imagine this wouldn't 
be too hard.

it seems that m[1] should be m.group(1), and everything else should 
follow.  i forgot about m[0] when making my slice proposal; i suppose 
then that m[:] should just do what we expect, and m[1:] = m.groups().  
len(m) = 1 + number of groups, m['name'] = m.group('name').

the only strangeness here is the numbering of groups starting at 1, and 
making 0 be a special case.  this isn't any more (or less) of a problem 
for the indexing form than it is for m.group(), and it's well known from 
various other languages.  we could always consider making groups start 
at 0 for python 3000, but this seems to me like a gratuitous 
incompatibility with the rest of the world.

ben





More information about the Python-Dev mailing list