On Thu, Aug 27, 2020, 1:29 AM Alexandre Brault <abrault@mapgears.com> wrote:
On 2020-08-27 12:33 a.m., Ricky Teachey wrote:
On Wed, Aug 26, 2020 at 10:34 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Wed, Aug 26, 2020 at 12:32:56PM -0400, Ricky Teachey wrote:
> It creates a language supported way for the creator of the class to decide
> how to interpret the contents inside a subscript operation.

We already have that: `__getitem__`.

Actually no, we have THREE dunders: get, set, and del -item.

I'm not seeing what problem adding a new dunder and indirection of __*item__ solves...

It kills at least 3 birds with one stone:

1. Brings kwd arguments to item dunders (PEP 472 does this too, but a key/index translation dunder kills two other birds)

2. Switches positional arguments over to the function paradigm, bringing the full power of python signature parsing to subscripting.

3. Removes the need to write calls to the same supporting function in the item dunders. They are called automatically.

Sure, on a pedantic level I had to put effort across three dunders, but the effort is a single method call *and* I would still have needed to do it in the __subscript__ scenario except I would also have to have written a __subscript__ that is a combination of _make_key and boilerplate to call the method that the interpreter would have previously called for me.

Alex

I don't care for how I wrote the details of how __subscript__ passes the args and kwargs to the item dunders, by calling them directly. Looking for another logical way to do that. 

My current attempt is this:

def __subscript__(self, *args, **kwargs) -> Tuple [Tuple[Any], Dict[str, Any]]:
    return t

Which, for a getitem dunder call, `t` becomes:

obj.__getitem__(*t[0], **t[1])