[Python-ideas] More useful slices

Todd toddrjen at gmail.com
Sun Feb 1 22:18:33 CET 2015


On Feb 1, 2015 6:27 PM, "Steven D'Aprano" <steve at pearwood.info> wrote:
>
> On Sun, Feb 01, 2015 at 04:13:32PM +0100, Todd wrote:
> > Although slices and ranges are used for different things and implemented
> > differently, conceptually they are similar: they define an integer
sequence
> > with a start, stop, and step size.
>
> I'm afraid that you are mistaken. Slices do not necessarily define
> integer sequences. They are completely arbitrary, and it is up to the
> object being sliced to interpret what is or isn't valid and what the
> slice means.
>
> py> class K(object):
> ...     def __getitem__(self, i):
> ...             print(i)
> ...
> py> K()["abc":(2,3,4):{}]
> slice('abc', (2, 3, 4), {})
>
>
> In addition, the "stop" argument to a slice may be unspecified until
> it it applied to a specific sequence, while range always requires
> stop to be defined.
>
> py> s = slice(1, None, 2)
> py> "abcde"[s]
> 'bd'
> py> "abcdefghi"[s]
> 'bdfh'
>
> It isn't meaningful to define a range with an unspecified stop, but it
> is meaningful to do so for slices.
>
>
> In other words, the similarity between slices and ranges is not as close
> as you think.
>

Fair enough.  But it doesn't really affect my proposal.  It would just be
that this syntax only allows a subset of what you can do with slices.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150201/abde7f77/attachment.html>


More information about the Python-ideas mailing list