I am having some difficulty behaviour of keyword only arguments feature in python3 when used with partial. Other info on keyword only arguments.
Here is my code:
def awesome_function(a = 0, b = 0, *, prefix):
print('a ->', a)
print('b ->', b)
print('prefix ->', prefix)
return prefix + str(a+b)
Here is my understanding of partial:
>>> two_pow = partial(pow, 2)
>>> two_pow(5)
32
>>>
What I understood is in the above example, partial
makes the second argument to pow
function as the only argument of two_pow
.
My question is why does the following work:
>>> g = partial(awesome_function, prefix='$')
>>> g(3, 5)
a -> 3
b -> 5
prefix -> $
'$8'
>>>
But I get error in this:
>>> awesome_function(prefix='$', 3, 5)
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg
>>>
I know that I can call awesome_function
directly by
>>> awesome_function(prefix='$', a = 3, b = 5)
a -> 3
b -> 5
prefix -> $
'$8'
>>>