[Python-ideas] More useful slices
Eugene Toder
eltoder at gmail.com
Sun Feb 1 17:37:24 CET 2015
On Sun, Feb 1, 2015 at 10:13 AM, Todd <toddrjen at gmail.com> 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. For this reason I think that slices
> could provide useful syntactic sugar for defining ranges without
> sacrificing clarity.
>
> The syntax I propose is simply to wrap a slice in parentheses. For
> examples, the following pairs are equivalent:
>
> range(4, 10, 2)
> (4:10:2)
>
> range(3, 7)
> (3:7)
>
> range(5)
> (:5)
>
> This syntax should not be easily confused with any existing syntax.
> Slices of sequences use square brackets, dictionaries use curly braces, and
> function annotations only appear in function declarations. It is also
> considerably more compact than the current range syntax, while still being
> similar to a syntax (slicing) all python users should be familiar with.
>
It may be too late for this, but I like it. We can also elide extra
parentheses inside calls, like with generator expressions:
sum(4:10:2)
Also, using list comprehension to generator expression analogy, we can have
[4:10:2]
which produces a list instead of a range object. Other than for
completeness this does not seem very useful, though.
Eugene
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150201/34511501/attachment.html>
More information about the Python-ideas
mailing list