[Python-ideas] More useful slices
Todd
toddrjen at gmail.com
Mon Feb 2 12:19:17 CET 2015
On Mon, Feb 2, 2015 at 11:53 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Feb 2, 2015 at 9:26 PM, Todd <toddrjen at gmail.com> wrote:
> > First, it wouldn't be a replacement. The existing range syntax would
> still
> > exist.
> >
> > But the reason it is beneficial is the same reason we have [a, b, c] for
> > list, {a:1, b:2, c:3} for dicts, {a, b, c} for sets, and (a, b, c) for
> > tuples. It is more compact way to create a commonly-used data structure.
> >
> > And I wouldn't consider it any more cryptic than any other literal we
> have.
>
> Considering the single most common use of ranges, let's see how a for
> loop would look:
>
> for i in 1:10:
> pass
>
> Is that nastily cryptic, or beautifully clean? I'm inclined toward the
> former, but could be persuaded.
>
>
>
It would be
for i in (1:10):
pass
Anyone who uses sequences should be familiar with this sort of notation:
for i in spam[1:10]:
pass
So should have at least some understanding that 1:10 can mean "1 to 10, not
including 10".
I don't see it being any more cryptic than {'a': 1, 'b': 2} meaning
dict(a=1, b=2). On the contrary, I think the dict literal syntax is even
more cryptic, since it has no similarity to any other syntax. The syntax I
propose here is at least similar (although not identical) to slicing.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150202/f22a7112/attachment.html>
More information about the Python-ideas
mailing list