[Python-ideas] slice.literal notation

Andrew Barnert abarnert at yahoo.com
Thu Jun 11 10:38:24 CEST 2015

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...)

> zero = operator.itemkey[0]
> ellipsis = operator.itemkey[...]
> reverse = slice(None, None, -1)
> reverse = operator.itemkey[::-1]
> all_rows_first_col = slice(None), slice(0)
> all_rows_first_col = operator.itemkey[:, 0]
> first_row_all_cols_but_last = slice(0), slice(None, -1)
> first_row_all_cols_but_last = operator.itemkey[0, :-1]
> Documentation would say that indexing into this object produces the
> result of the key transformation step of getitem/setitem/delitem
> Cheers,
> Nick.
> -- 
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

More information about the Python-ideas mailing list