[Python-ideas] slice.literal notation
Tal Einat
taleinat at gmail.com
Thu Jun 11 14:57:02 CEST 2015
On Thu, Jun 11, 2015 at 11:38 AM, Andrew Barnert <abarnert at yahoo.com> wrote:
> On Jun 10, 2015, at 18:54, Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>> I'm with Tal here - I like the concept, don't like the spelling
>> because it may return things other than slice objects.
>>
>> While the formal name of the operation denoted by trailing square
>> brackets "[]" is "subscript" (with indexing and slicing being only two
>> of its several use cases), the actual *protocol* involved in
>> implementing that operation is getitem/setitem/delitem, so using the
>> formal name would count as "non-obvious" in my view.
>>
>> Accordingly, I'd suggest putting this in under the name
>> "operator.itemkey" (no underscore because the operator module
>> traditionally omits them).
>
> That name seems a little odd. Normally by "key", you mean the thing you subscript a mapping with, as opposed to an index, the thing you subscript a sequence with (either specifically an integer, or the broader sense of an integer, a slice, an ellipsis, or a tuple of indices recursively).
>
> (Of course you _can_ use this with a mapping key, but then it just returns the same key you passed in, which isn't very useful, except in allowing generic code that doesn't know whether it has a key or an index and wants to pass it on to a mapping or sequence, which obviously isn't the main use here.)
>
> "itemindex" avoids the main problem with "itemkey", but it still shares the secondary problem of burying the fact that this is about slices (and tuples of plain indices and slices), not just (or even primarily) plain indices.
>
> I agree with you that "subscript" isn't a very good name either.
>
> I guess "lookup" is another possibility, and it parallels "LookupError" being the common base class of "IndexError" and "KeyError", but that sounds even less meaningful than "subscript" to me.
>
> So, I don't have a good name to offer.
>
> One last thing: Would it be worth adding bracket syntax to itemgetter, to make it easier to create slicing functions? (That wouldn't remove the need for this function, or vice versa, but since we're in operator and adding a thing that gets "called" with brackets...)
I actually think "subscript" is quite good a name. It makes the
explicit distinction between subscripts, indexes and slices.
As for itemgetter, with X (placeholder for name we choose), you would
just do itemgetter(X[::-1]), so I don't see a need to change
itemgetter.
- Tal
More information about the Python-ideas
mailing list