[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 [1]
or use keyword arguments [2] :

def f(x, *, long_name='foo'): return ...

f(2, long_name='bar')  # [1]
f(long_name='bar', x=2)  # [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 :

import math
math.hypot(y=5, x=2)  # TypeError

A solution is to use partial.

from functools import partial
partial(f, long_name='bar')(2)

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, )
f(*a, **k)

So what would be a good new syntax for that or workaround I didn't think of

f(**dict(long_name='bar'), *(2, ))

f(long_name='bar', 0=2)
math.hypot(0=5, 1=2)

f(long_name='bar', args[0]=2)
math.hypot(args[0]=5, args[1]=2)


I don't know if such an idea has been asked before (I don't know how to
formulate it).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190515/e427f9bf/attachment.html>

More information about the Python-ideas mailing list