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

Talin talin at acm.org
Thu Dec 7 19:24:02 CET 2006

Fredrik Lundh wrote:
> Talin wrote:
>> Maybe instead of considering a match object to be a sequence, a match 
>> object should be considered a map?
> sure, except for one small thing.  from earlier in this thread:
>  > Ka-Ping Yee wrote:
>  >
>  >> I'd say, don't pretend m is a sequence.  Pretend it's a mapping.
>  >> Then the conceptual issues go away.
> to which I replied:
>  > almost; that would mean returning KeyError instead of IndexError for
>  > groups that don't exist, which means that the common pattern
>  >
>  >      a, b, c = m.groups()
>  >
>  > cannot be rewritten as
>  >
>  >      _, a, b, c = m
>  >
>  > which would, perhaps, be a bit unfortunate.

I think the confusion lies between the difference between 'group' (which 
takes either an integer or string argument, and behaves like a map), and 
'groups' (which returns a tuple of the numbered arguments, and behaves 
like a sequence.)

The original proposal was to make m[n] a synonym for m.group(n). 
"group()" is clearly map-like in its behavior.

It seems to me that there's exactly three choices:

   -- Match objects behave like 'group'
   -- Match objects behave like 'groups'
   -- Match objects behave like 'group' some of the time, and like 
'groups' some of the time, depending on how you refer to it.

In case 1, a match object is clearly a map; In case 2, it's clearly a 
sequence; In case 3, it's neither, and all talk of consistency with 
either map or sequence is irrelevant.

-- Talin

More information about the Python-Dev mailing list