[Python-ideas] Passing positional arguments as keyword arguments (to provide function arguments out of order)
Robert Vanden Eynde
robertve92 at gmail.com
Tue May 14 21:07:54 EDT 2019
Currently if one wants to provide positional arguments after keyword
arguments, it's not possible, one must begin with positional arguments 
or use keyword arguments  :
def f(x, *, long_name='foo'): return ...
f(2, long_name='bar') # 
f(long_name='bar', x=2) # 
The problem is that it's not always possible to do so because some
functions cannot have keyword arguments (because they call a C function)
like math.hypot :
math.hypot(y=5, x=2) # TypeError
A solution is to use partial.
from functools import partial
But that begins to be "functional programming style" and that doesn't solve
the case where one would like to change the order of (I created
funcoperators.elipartial for that reason) :
from funcoperators import elipartial
elipartial(math.hypot, ..., 5)(2)
Of course one could create a tuple and a dict they call f(*a, **k) but
cannot be used inside an expression.
k = dict(long_name='bar')
a = (2, )
So what would be a good new syntax for that or workaround I didn't think of
f(**dict(long_name='bar'), *(2, ))
I don't know if such an idea has been asked before (I don't know how to
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas