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

Guido van Rossum guido at python.org
Sat Nov 12 20:08:02 EST 2016


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/aecf4b66/attachment-0001.html>


More information about the Python-ideas mailing list