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

Alastair Houghton alastair at alastairs-place.net
Fri Dec 8 12:11:00 CET 2006


On 7 Dec 2006, at 21:47, Josiah Carlson wrote:

> Alastair Houghton <alastair at alastairs-place.net> wrote:
>> On 7 Dec 2006, at 02:01, Josiah Carlson wrote:
>>> Alastair Houghton <alastair at alastairs-place.net> wrote:
>>>> On 7 Dec 2006, at 01:01, Josiah Carlson wrote:
>>>>> If we don't want
>>>>> slicing, or if prodicing a slice would produce a semantically
>>>>> questionable state, then lets not do it.
>>>>
>>>> ...if you return match objects from slicing, you have problems  
>>>> like m
>>>> [::-1].groups().  *I* don't know what that should return.
>>>
>>> I would argue that any 'step' != 1 has no semantically correct  
>>> result
>>> for slicing on a match object, so we shouldn't support it.
>>
>> OK, but even then, if you're returning a match object, how about the
>> following:
>>
>>>>> m = re.match('(A)(B)(C)(D)(E)', 'ABCDE')
>>>>> print m[0]
>>    ABCDE
>>>>> n = m[2:5]
>>>>> print list(n)
>>    ['B', 'C', 'D']
>>>>> print n[0]
>>    B
>>>>> print n.group(0)
>>    B
>>
>> The problem I have with it is that it's violating the invariant that
>> match objects should return the whole match in group(0).
>
> If we were going to go with slicing, then it would be fairly  
> trivial to
> include the whole match range.  Some portion of the underlying  
> structure
> knows where the start of group 2 is, and knows where the end of  
> group 5
> is, so we can slice or otherwise use that for subsequent sliced  
> groups.

But then you're proposing that this thing (which looks like a tuple,  
when you're indexing it) should slice in a funny way.  i.e.

   m = re.match('(A)(B)(C)(D)(E)', 'ABCDE')
   print m[0]
   ABCDE
   print list(m)
   ['ABCDE', 'A', 'B', 'C', 'D', 'E']

   n = m[2:5]
   print list(n)
   ['BCD', 'B', 'C', 'D']
   print len(n)
   4

   p = list(m)[2:5]
   print p
   ['B', 'C', 'D']
   print len(p)

Or are you saying that

   m[2:5][0] != m[2:5].group(0)

but

   m[0] == m.group(0)

??

Either way I think that's *really* counter-intuitive.

Honestly, I don't think that slicing should be supported if it's  
going to have to result in match objects, because I can't see a way  
to make them make sense.

I think that's Frederik's objection also, but unlike me he doesn't  
feel that the slice operation should return something different (e.g.  
a tuple).

Kind regards,

Alastair.

--
http://alastairs-place.net




More information about the Python-Dev mailing list