On Sat, Aug 29, 2020, at 10:12, Ricky Teachey wrote: > If we add kwargs to the subscript operator, we'll be able to add new > required or optional names to item dunder signatures and supply named > arguments : > > def __getitem__(self, key, x, y): ... > > q[x=1, y=2] > > But if we want to have the same behavior without supporting function > style syntax, we will have to write code like this: > > MISSING = object() > > def __getitem__(self, key, x=MISSING, y=MISSING): > if x is MISSING and y is MISSING:: > x, y = key > if x is missing: > x, = key > if y is MISSING: > y, = key
[side note: I don't believe the behavior suggested by this last "if y is MISSING:" clause is supported by your proposal either. It's certainly not supported by function calls. Are you suggesting q[x=1, 2], or q[2, x=1] to be equivalent to y=2?] def _getitem(self, x=..., y=...): ... def __getitem__(self, arg, /, **kwargs): if isinstance(arg, tuple): return self._getitem(*arg, **kwargs) else: return self._getitem(arg, **kwargs) or, as a more compact if slightly less efficient version def __getitem__(self, arg, /, **kwargs): return self._getitem(*(arg if isinstance(arg, tuple) else (arg,)), **kwargs) This is only a few lines [which would have to be duplicated for set/del, but still], you're free to implement it on your own if necessary for your use case. My code assumes that no positional arguments results in an empty tuple being passed - it would be slightly more complex but still manageable if something else is used instead. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/XEMYGQDSEUOIN3RZMORJLWTNS3JUH6C4/ Code of Conduct: http://python.org/psf/codeofconduct/