On Sat, Sep 26, 2020 at 7:51 AM Stefano Borini <stefano.borini@gmail.com> wrote:
On Sat, 26 Sep 2020 at 04:02, Steven D'Aprano <steve@pearwood.info> wrote:

> Did you just completely undermine the rationale for your own PEP?
> Isn't the entire purpose of this PEP to allow subscripts to include
> keyword arguments? And now you are describing it as "poor design"?

Not really. to _me_, an indexing operation remains an indexing
operation. My personal use cases are two:

1. naming axes (e.g. replace, if desired obj[1, 2] with obj[row=1, col=2])
2. typing generics MyType[T=int]

In this fashion have you considering having keyword only indices, that is to only allow either obj[1, 2] or obj[row=1, col=2] (if the class supports it), and disallow mixing positional and keyword indices, meaning obj[1, col=2] would be a SyntaxError.

If we followed that path, then adding a new set of dunders may not be that problematic as the use case would be slightly different than the current semantics.

One could implement the current set of dunders __[get|set|del]item__ in case you want to support keywordless indexing, and this hypothetical new set of dunders if you wanted to support keyword indices. If you want both you need to implement both. However, a decorator may be added to easily allow both semantics.

Has anyone provided compelling use cases for mixing indices with and without keywords?

I also agree with Stefano that something like a[1, 2, unit="meters"] feels really odd, but maybe by adding the names to the first 2 dimensions the intent could be clearer.

Other use cases are certainly allowed, but to me, something like

a[1, 2, unit="meters"]

makes me feel uncomfortable, although I might learn to accept it. In
particular, the above case becomes kind of odd when you use it for
setitem and delitem

a[1, 2, unit="meters"] = 3

what does this mean? convert 3 to meters and store the value in a?
Then why isn't the unit close to 3, as in

a[1,2] = 3 * meters

and what about this one?

del a[1, 2, unit="meters"] # and this one?

I feel that, for some of those use cases (like the source one),
there's a well established, traditional design pattern that fits it
"better" (as it, it feels "right", "more familiar")

> I'm not really sure why this hypothetical call:
>     snapshot1 = remote_array[300:310, 50:60, 30:35, source=worker1]
> is "abuse" or should make us more uneasy that this hypothetical call:

I don't know... it just doesn't feel... "right" :) but maybe there's a
logic to it.
You are indexing on the indexes, and also on the source.

Yeah, makes sense.


Kind regards,

Stefano Borini
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/IUGWBNNFF275HVRNDRJ2RWR53ZJYMPBN/
Code of Conduct: http://python.org/psf/codeofconduct/

Sebastian Kreft