[Python-ideas] str(slice(10)) should return "slice(10)"
David Mertz
mertz at gnosis.cx
Sat Nov 12 20:46:01 EST 2016
If we *do* want the name 'slice' as the spelling for the thing that can
either be called or indexed to create a slice object, we could probably use
some boilerplate like this:
In [1]: class Slice:
...: def __init__(self):
...: self.slice = slice
...: def __getitem__(self, x):
...: return x
...: def __call__(self, *args, **kws):
...: return self.slice(*args, **kws)
...:
In [2]: slice = Slice()
In [3]: slice(1,10,2)
Out[3]: slice(1, 10, 2)
In [4]: slice[1:10:2]
Out[4]: slice(1, 10, 2)
I'm sure there are some less common uses of the name 'slice' that would
break here. That's why I'd want an official standard behavior.
> 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:
>
>> 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:
>>
>
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161112/697b77e8/attachment-0001.html>
More information about the Python-ideas
mailing list