[Python-ideas] slice.literal notation
Nick Coghlan
ncoghlan at gmail.com
Thu Jun 11 03:54:03 CEST 2015
On 11 June 2015 at 06:21, Tal Einat <taleinat at gmail.com> wrote:
> On Wed, Jun 10, 2015 at 10:05 PM, David Mertz <mertz at gnosis.cx> wrote:
>> This is an elegant improvement that doesn't affect backward compatibility.
>> Obviously, the difference between the spelling 'sliceliteral[::-1]' and
>> 'slice.literal[::-1]' isn't that big, but having it attached to the slice
>> type itself rather than a user class feels more natural.
>
> I dislike adding this to the slice class since many use cases don't
> result in a slice at all. For example:
>
> [0] -> int
> [...] -> Ellipsis
> [0:1, 2:3] -> 2-tuple of slice object
>
> I like NumPy's name of IndexExpression, perhaps we can stick to that?
>
> As for where it would reside, some possibilities are:
>
> * the operator module
> * as part of the collections.abc.Sequence abstract base class
> * the types module
> * builtins
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).
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
More information about the Python-ideas
mailing list