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 email@example.com wrote:
On 2 Feb 2021, at 12:36, Stefano Borini wrote:
I would like to request feedback by python-dev on the current implementation of PEP 637 - Support for indexing with keyword arguments.
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 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 = 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: @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 _______________________________________________ Python-Dev mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://firstname.lastname@example.org/message/DTOO36EX... Code of Conduct: http://python.org/psf/codeofconduct/