On Mon, Aug 24, 2020 at 9:26 AM Sebastian Kreft <skreft@gmail.com> wrote:
As I mentioned in another thread, I think the syntax in which the initial argument of the slice is missing may be visually confusing, as it is too similar to the walrus operator.

d[x=:3] or d[x=:]

which I suppose is the point mentioned: "what if we want to use colons for other things"?

However, the second case will be usually formatted as x = +3, as per PEP 8

So unless PEP8 is updated to require/suggest spaces around a keyword index (which I'm not proposing), then I am -1 for the suggested feature, at least when the initial element is missing.

PEP8 can't require, and of course it could be updated -- that seems a non issue.

As for "why not" not being a motivator -- I agree, I posted it that easy because this conversation has brought up a number of examples where slice syntax is nice to use. And David Mertz pointed out, both numpy and pandas have a utility to make easier slices -- yes, that's an argument for why you don't need them, but it's also an argument for why there IS a need for slice objects outside of the square brackets, and if we need slice objects, then slice syntax is nice.

But rather than suggesting one or two places where we might use sloce syntax: e.g. function calls:

object.get_subset(x=a:b, y=c:d), or itertools,islice(it, 2:20:2)

Once you allow them anywhere outside  [], then the question does become "why not?", because having an expression that is only valid in some small fraction of contexts gets even more confusing.

Of course, as has been pointed out here -- dict displays are one good reason NOT to support it.

Which is too bad -- I'd really like to see it more broadly available -- I think it's a very nice syntax.

I am strongly in favor of having slices.  The main motivating factor for me, labelled dimensions in xarray, would be much, much less useful without support for slices.  In fact, as PEP 472 currently mentions, the big benefit of indexing over method calls is that indexing supports slice syntax while method calls don't.

so maybe the solution is to have method calls support slices :-)

-CHB

 

In a more general sense, I feel not allowing slices would create an artificial distinction between labelled and positional indices that I don't think is justified.  They would work the same, except for slices where labelled indices behave differently.  It would be a strange gotcha.

So I think any revision to PEP 472 or new PEP should directly and explicitly support the use of slices.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/TOABKD7A5X653BTTU3MZICWURNGPMY47/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Sebastian Kreft
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/IQJNOQS2LKHWSSYAJX7KDNWJ5ZFZ25N4/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython