On Dec 12, 2019, at 05:29, Siddharth Prajosh <spraj...@gmail.com> wrote:
>
> Python slicing looks really weird. How do I explain s=list(range(100));
> s[10:20] gives you a part of the list.
Well, first you have to explain what “slicing” means, and that for lists it
returns a new copy of the sub-list, and that it’s a half-open range that
includes the 10th element but not the 20th, and that 10th and 20th are counted
from 0 rather than from 1, and that indexing (including slicing) is spelled
with square brackets rather than subscripts like in math, and so on, because a
non-programmer isn’t going to know any of those things.
Once you explain all that, I don’t see why explaining that slicing is spelled
with a colon is any more difficult.
> Can we have an extra function for lists and string (and wherever slicing
> works)
“Wherever slicing works” includes zillions of third-party types. How are you
going to change all of those?
And how do you want to spell something like the numpy multidimensional indexing
a[10:20, 3, ...,10:20]? Or the dpath (or one of those json-dogging libraries; I
always mix them up) search a[“thingies”, 1:3, “name”] (which is roughly
equivalent to [thingy[“name”] for thingy in a[“thingies”][1:3]])?
> to explicitly mention that we're slicing?? Something like -
> s=list(range(100)); s.slice(10, 20).
>
> Has this been mentioned before? What would be a drawback of having something
> more readable like a .slice function?
Since you’re quoting the Zen, TOOWTDI is the obvious answer here. Everyone
would still have to learn slicing syntax to read the zillions of existing lines
of Python that use it, and all the new code people will continue to write. But
now they’d also have to learn to read the slice method. That’s more to learn,
not less.
Plus, everyone who implements a sequence type would have to implement both ways
of doing it. (Since slicing isn’t required to be a sequence, the Sequence ABC
probably couldn’t check this for you, and, when used as a mixin, couldn’t
implement it for you.)
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/WILGYAPJZI4E72QWRR3FGCYH3V7P5UBE/
Code of Conduct: http://python.org/psf/codeofconduct/