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
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
selfin a method call is done: By passing an additional positional argument (in this case as the second argument after
type(foo).__getitem__(foo, 1, 2)
type(foo).__getitem__(foo, (1, 2))
foo = 3is
type(foo).__setitem__(foo, 3, 1)
foo[1, 2] = 3is
type(foo).__setitem__(foo, 3, 1, 2)
foo[(1, 2)] = 3is
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.
__delitem_ex__) for performance reasons, but would it make sense, to mark existing
__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.
Python-Dev mailing list -- email@example.com
To unsubscribe send an email to firstname.lastname@example.org
Message archived at https://email@example.com/message/DTOO36EXJRBGA7OJVTRAE7I43D2FR7BS/
Code of Conduct: http://python.org/psf/codeofconduct/