We have examined every possible permutation already, I think this would be a distraction to pursue.

On Thu, Feb 4, 2021 at 04:20 Walter Dörwald <walter@livinglogic.de> wrote:

On 2 Feb 2021, at 12:36, Stefano Borini wrote:

Hi all,

I would like to request feedback by python-dev on the current
implementation of PEP 637 - Support for indexing with keyword


The PEP is ready for SC submission and it has a prototype
implementation ready, available here (note, not reviewed, but
apparently fully functional)


(note: not sure if there's a preference for the link to be to the diff
or to the branch, let me know if you prefer I change the PEP link)

It seems to me, that what complicates the specification is the need for backwards compatibility. If that wasn't an issue, we could make indexing operations behave exactly like function calls. Handling the additional argument for __setitem__ could be done the same way that passing self in a method call is done: By passing an additional positional argument (in this case as the second argument after self), So:

  • foo[1] is type(foo).__getitem__(foo, 1)
  • foo[1, 2] is type(foo).__getitem__(foo, 1, 2)
  • foo[(1, 2)] is type(foo).__getitem__(foo, (1, 2))
  • foo[1] = 3 is type(foo).__setitem__(foo, 3, 1)
  • foo[1, 2] = 3 is type(foo).__setitem__(foo, 3, 1, 2)
  • foo[(1, 2)] = 3 is type(foo).__setitem__(foo, 3, (1, 2))

But of course this isn't backwards compatible with respect to the treatment of tuple arguments and the argument order in __setitem__. However it is much easier to remember and to teach.

The PEP rejects the idea to implement this approach via a new set of dunder methods (e.g. __getitem_ex__, __setitem_ex__ and __delitem_ex__) for performance reasons, but would it make sense, to mark existing __getitem__, __setitem__ and __delitem__ methods as supporting the new calling convention via a decorator? i.e something like:

class X:
    def __getitem__(self, x, y, z=42):

    def __setitem__(self, value, x, y, z=42):

    def __detitem__(self, x, y, z=42):

This wouldn't require an additional dictionary lookup, but just a check of a bit in the function object.

Thank you for your help.


Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/DTOO36EXJRBGA7OJVTRAE7I43D2FR7BS/
Code of Conduct: http://python.org/psf/codeofconduct/
--Guido (mobile)