On Sun, Aug 23, 2020 at 9:42 PM Todd <toddrjen@gmail.com> wrote:
I think it is worth directly discussing the availability of slices in PEP 472-style keyword indices, since we seem to have mostly converged on a dunder method signature.  This is an issue that has been alluded to regarding keyword-based (labelled) indices but not directly addressed.  The basic syntax would be something like d[x=1:3].

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=:]

There's precedent for combinations of symbols that have different meanings when swapped. For example:

x += 3 vs x =+ 3

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.
 

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.

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