[Python-ideas] Fwd: Keyword only argument on function call

David Mertz mertz at gnosis.cx
Tue Sep 25 07:47:27 EDT 2018


I'm still not sure why all this focus on new syntax or convoluted IDE
enhancements. I presented a very simple utility function that accomplishes
exactly the started goal of DRY in keyword arguments.

Yes, I wrote a first version that was incomplete. And perhaps these 8-9
lines miss some corner case. But the basic goal is really, really easy to
accomplish with existing Python.

>>> import inspect
>>> def reach(name):
...     for f in inspect.stack():
...         if name in f[0].f_locals:
...             return f[0].f_locals[name]
...     return None
...
>>> def use(names):
...     kws = {}
...     for name in names.split():
...         kws[name] = reach(name)
...     return kws
...
>>> def function(a=11, b=22, c=33, d=44):
...     print(a, b, c, d)
...
>>> function(a=77, **use('b d'))
77 None 33 None
>>> def foo():
...     a, b, c = 1, 2, 3
...     function(a=77, **use('b d'))
...
>>> foo()
77 2 33 None


On Tue, Sep 25, 2018, 6:31 AM Anders Hovmöller <boxed at killingar.net> wrote:

> Hi,
>
> I'd like to reopen this discussion if anyone is interested. Some things
> have changed since I wrote my original proposal so I'll first summarize:
>
> 1. People seem to prefer the syntax `foo(=a)` over the syntax I suggested.
> I believe this is even more trivial to implement in CPython than my
> original proposal anyway...
> 2. I have updated my analysis tool:
> https://gist.github.com/boxed/610b2ba73066c96e9781aed7c0c0b25c  It will
> now also give you statistics on the number of arguments function calls
> have. I would love to see some statistics for other closed source programs
> you might be working on and how big those code bases are.
> 3. I have made a sort-of implementation with MacroPy:
> https://github.com/boxed/macro-kwargs/blob/master/test.py I think this is
> a dead end, but it was easy to implement and fun to try!
> 4. I have also recently had the idea that a foo=foo type pattern could be
> handled in for example PyCharm as a code folding feature (and maybe as a
> completion feature).
>
> I still think that changing Pythons syntax is the right way to go in the
> long run but with point 4 above one could experience what this feature
> would feel like without running a custom version of Python and without
> changing your code. I admit to a lot of trepidation about wading into
> PyCharms code though, I have tried to do this once before and I gave up.
>
> Any thoughts?
>
> / Anders
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180925/4c587207/attachment-0001.html>


More information about the Python-ideas mailing list