On Mon, Aug 03, 2020 at 05:51:58PM -0400, Ricky Teachey wrote:

> However I'll also point out that another idea from Jonathan Fine has the
> potential to fix both this problem and the key object signature problem,
> which is what he called a "SIGNATURE CHANGING ADAPTER".
> 
> 
> > Here's how it goes. First we write
> >     class D:
> >         @wibble
> >         def __setitem__(self, val, u, v, x, y):
> >             pass  # Or do something.
> >
> > Next, we define wibble. It will be a SIGNATURE CHANGING ADAPTER.  Those
> > who know how to make decorators will, I hope, have little difficulty in
> > defining wibble to do what is required. For this exercise, assume that
> > k.argv = (1, 2), and k.kwargs = dict(x=3, y=4).

Good news! Thanks to Guido's time machine, Python already supports this 
signature changing adapter for functions and methods. All you have to do 
is *leave the decorator out*. In fact you don't even have to define it 
at all. The Python interpreter already knows how to match up keyword 
arguments like `u=35` and their associated parameters `u`.

So we don't have to duplicate the argument parsing logic of the 
interpret in a decorator. We just have to let the interpreter do its 
thing like it already does.

This is (almost) as flexible as you want:

Want your keyword arguments to be optional? Define them with a default. 
Want them to be mandatory? No default. Want to accept arbitrary keyword 
arguments? Include a `**kwargs` parameter. Want all of your keywords 
bundled together? Use only a `**kwargs` parameter.

The only limitation is that subscripting syntax bundles all positional 
arguments into a single parameter, for reasons lost deep in the mists of 
time. But that doesn't affect keyword parameters.


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

Reply via email to