[Python-ideas] str(slice(10)) should return "slice(10)"

David Mertz mertz at gnosis.cx
Sat Nov 12 20:28:16 EST 2016


The very common use case for creating slice objects is in Pandas and
similar libraries. Xarray certainly, or Blaze, to a lesser extent NumPy.

That said, it's very easy to define a suitable __getitem__, as Guido shows.
It's really a question simply of whether that object should be named
'slice' or something else.

On Nov 12, 2016 5:08 PM, "Guido van Rossum" <guido at python.org> wrote:

> On Sat, Nov 12, 2016 at 11:41 AM, Ivan Levkivskyi <levkivskyi at gmail.com>
> wrote:
>
>> On 12 November 2016 at 20:27, David Mertz <mertz at gnosis.cx> wrote:
>>
>>> I thought of the use of `.__getitem__()` in metaclasses in the typing
>>> module.  I feel like this use is more natural and more useful than that.
>>> Should we someday need a slice generic type for PEP 484, the spelling would
>>> naturally be `Slice[T]` instead, in my mind.  But `slice[1:10,2]` should be
>>> a constructor for a concrete slice object.
>>>
>>
>> Slice[T] vs slice[::-1] is coherent with what we have now for List[T] vs
>> list, etc.
>>
>
> Not really. We have List[T] but list[x] is invalid -- it doesn't have a
> different meaning (it's list instances that support indexing). And in fact
> the distinction between List and list is intentionally minimal -- List is
> simply what list wants to become when it grows up. :-)
>
> Honestly I think the use case of wanting to create a slice object is rare
> enough that we can continue to write slice(x, y, z). If you really find
> yourself wanting something shorter, I believe in the past it's been pointed
> out that you could create a helper, e.g. like this:
>
> class S:
>     def __getitem__(self, x): return x
> s = S()
>
> a = s[:():]
>
> --
> --Guido van Rossum (python.org/~guido)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161112/290e4279/attachment.html>


More information about the Python-ideas mailing list