On Sat, 29 Aug 2020 at 15:12, Ricky Teachey <ri...@teachey.org> wrote:
> Here's one reason I find a new dunder or dunders compelling that I haven't 
> seen anyone respond to directly:
>
> I can write functions that define named arguments, and if I pass them 
> positionally, they get assigned the right name automatically (unless 
> disallowed by the signature using 3.8 positional only syntax):
>
> def f(x, y): ...
>
> f(1, 2)
> f(1, y=2)
> f(y=2, x=1)
>
> 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
>
> And probably that code I just wrote has bugs. And it gets more complicated if 
> we want to have more arguments than just two. And even more complicated if we 
> want some of the arguments to be positional only or any other combination of 
> things.
>
> This is code you would not have to write if we could do this instead with a 
> new dunder or subscript processor:
>
> def __getx__(self, x, y): ...
>
> And these all just work:
>
> q[1, 2]
> q[1, y=2]
> q[y=2, x=1]
>
> 1 is assigned to x and 2 is assigned to y in all of these for both versions, 
> but the second certain requires not parsing of parameters. Python does it for 
> us. That's a lot of easily available flexibility.

But you don't give any reason why you'd want to do that. Why are you
using subscript notation rather than a simple function call? This is
just another variation on the "it would be nice if..." argument, which
has been covered over and over again. What is the use case here, and
why is the proposed solution more compelling than anything that can
already be done in Python? Not simply compelling in the sense of "it
looks nice", but how does subscript notation map to the problem
domain, and how would all the variations you say would "just work" be
meaningful in terms of the real-world problem?

The question isn't whether named arguments are useful - function calls
demonstrate that perfectly well. The question is why are they needed
*for subscripting*, and what do they *mean* when used in applications
where subscripts are the natural way to express the problem logic?

Paul
_______________________________________________
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/B6GIWT2JTEZ5KEKO6RB2J4VYI4UMD2PH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to