[Python-ideas] Keyword only argument on function call
boxed at killingar.net
Thu Sep 6 06:15:46 EDT 2018
I have a working implementation for a new syntax which would make using keyword arguments a lot nicer. Wouldn't it be awesome if instead of:
foo(a=a, b=b, c=c, d=3, e=e)
we could just write:
foo(*, a, b, c, d=3, e)
and it would mean the exact same thing? This would not just be shorter but would create an incentive for consistent naming across the code base.
So the idea is to generalize the * keyword only marker from function to also have the same meaning at the call site: everything after * is a kwarg. With this feature we can now simplify keyword arguments making them more readable and concise. (This syntax does not conflict with existing Python code.)
The full PEP-style suggestion is here: https://gist.github.com/boxed/f72221e7e77370be3e5703087c1ba54d
I have also written an analysis tool you can use on your code base to see what kind of impact this suggestion might have. It's available at https://gist.github.com/boxed/610b2ba73066c96e9781aed7c0c0b25c . The results for django and twisted are posted as comments to the gist.
We've run this on our two big code bases at work (both around 250kloc excluding comments and blank lines). The results show that ~30% of all arguments would benefit from this syntax.
Me and my colleague Johan Lübcke have also written an implementation that is available at: https://github.com/boxed/cpython
/ Anders Hovmöller
More information about the Python-ideas