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 arguments.
https://www.python.org/dev/peps/pep-0637/
The PEP is ready for SC submission and it has a prototype implementation ready, available here (note, not reviewed, but apparently fully functional)
https://github.com/python/cpython/compare/master...stefanoborini:PEP-637-imp...
(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: ```python class X: @newstyle_item def __getitem__(self, x, y, z=42): ... @newstyle_item def __setitem__(self, value, x, y, z=42): ... @newstyle_item 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.
Servus, Walter