> Suppose we have
> >>> d[x=1, y=2] = 42
> >>> d[x=1, y=2]
> 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
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)].
Python-ideas mailing list -- firstname.lastname@example.org
To unsubscribe send an email to email@example.com
Message archived at https://firstname.lastname@example.org/message/TGNFCU24YK36BOLKULDB36JRW73VTJGC/
Code of Conduct: http://python.org/psf/codeofconduct/