[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
Thu Dec 7 13:28:56 CET 2006
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).
It's these kinds of things that make me think that slices shouldn't
have all of the methods of a match object. I think that's probably
why various others have suggested not supporting slicing, but I don't
think it's necessary to avoid it as long as it has clearly specified
behaviour.
>> If you're worried about types, you could do something like this:
>>
>> generic match object
>> |
>> +--------------+-------------+
>> | |
>> real match object match object slice
>
> I believe the above is unnecessary. Slicing a match could produce
> another match. It's all internal data semantics.
Sure. My point, though, was that you could view (from an external
perspective) all results as instances of "generic match object",
which might not have as many methods.
Interestingly, at present, the match object type itself is an
implementation detail; e.g. for SRE, it's an _sre.SRE_Match object.
It's only the API that's documented, not the type.
Kind regards,
Alastair.
--
http://alastairs-place.net
More information about the Python-Dev
mailing list