[Python-ideas] Keyword only argument on function call
desmoulinmichel at gmail.com
Sat Sep 8 13:05:09 EDT 2018
Le 06/09/2018 à 03:15, Anders Hovmöller a écrit :
> 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)
It will make code harder to read.
Indeed, now your brain has to make the distinction between:
foo(a, *, b, c)
foo(a, b, *, c)
Which is very subtle, yet not at all the same thing.
All in all, this means:
- you have to stop to get the meaning of this. Scanning the lines
doesn't work anymore.
- this is a great opportunity for mistakes, and hence bugs.
- the combination of the two makes bugs that are hard to spot and fix.
> 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
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas