On Sat, Aug 15, 2020 at 8:27 PM Guido van Rossum <guido@python.org> wrote:
On Sat, Aug 15, 2020 at 8:02 PM Todd <toddrjen@gmail.com> wrote:
On Sat, Aug 15, 2020 at 7:26 PM Stefano Borini <stefano.borini@gmail.com> wrote:
> Suppose we have
>     >>> d[x=1, y=2] = 42
>     >>> d[x=1, y=2]
>     42
> where d is an instance of a suitable class X that has no special knowledge of keywords.

Initially, when I wrote the pep, the idea was that there was no
distinction of kwargs and normal args. Basically the idea was that
currently the only "metainfo" associated to every argument is purely
positional (e.g. the meaning of position 1 is implicit). But index 1
can have a specific semantic meaning (e.g. it could be a day).
So in practice they would be one and the same, just that you add
non-positional semantic meaning to indexes, and you can refer to them
either through the position, or this additional semantic meaning.

In other words, if you claim that the first index is day, and the
second index is detector, somehow, there is no difference between

d[3, 4]
d[day=3, detector=4]
d[detector=4, day=3]

In fact, my initial feeling would be that you can use either one or
the other. You should not be able to mix and match.

the pep went through various revisions, and we came to a possible
proposal, but it's not set in stone.

This would definitely not be sufficient for xarray, which I see as being one of the main users of this syntax.  The whole point is to be able to specify an arbitrary subset labeled dimensions.

Are you saying that for xarray it is important to distinguish between `d[day=3, detector=4]` and `d[detector=4, day=3]`? If we just passed the keyword args to `__getitem__` as an extra `**kwds` argument (which preserves order, since Python 3.6 at least), that should work, right? If not, can you clarify?

An extra **kwds would be quite sufficient for xarray. We don't need to distinguish between `d[day=3, detector=4]` and `d[day=4, detector=3]`, at least not any differently from normal Python keyword arguments.

What might not suffice is a required one-to-one mapping between positional and keyword arguments. Xarray has a notion of a "Dataset" that contains multiple arrays, some of which may have different dimensions or dimensions in a different order. For this reason, on Dataset we allow keyword-based indexing, but not positional-based indexing. This is basically the same as the use-cases for keyword-only parameters.

One question that comes up: should d[**kwargs] be valid syntax? d[*args] currently is not, but that's OK since d[tuple(args)] is identical.

On the other hand, we probably do need d[**kwargs] since there's no way to dynamically unpack keyword arguments (short of directly calling __getitem__). And perhaps for symmetry this suggests d[*args] should be valid, too, defined as equivalent to d[tuple(args)].

--Guido van Rossum (python.org/~guido)
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/TGNFCU24YK36BOLKULDB36JRW73VTJGC/
Code of Conduct: http://python.org/psf/codeofconduct/