Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make python built-in functions support keyword arguments? [duplicate]

I want to use partial() to build a function that only takes one argument,
so that I can pass it to some high-order functions (ex: map()/filter()):


>>> from operator import sub
>>> from functools import partial

>>> map(lambda x:sub(x, 5), [1,2,3])
[-4, -3, -2]

>>> help(sub)
Help on built-in function sub in module operator:

sub(...)
    sub(a, b) -- Same as a - b.

>>> map(partial(sub, b=5), [1,2,3])
TypeError: sub() takes no keyword arguments

Is there some way to make sub()(or any other built-in functions) support keyword arguments?

like image 676
varwey Avatar asked Nov 11 '22 07:11

varwey


1 Answers

If a function defined in C does not take keyword arguments then there is no way to force it to do so. Either use lamdba and fill the arguments in the hard way, or wrap the function in a Python function that can take keyword arguments.

like image 69
Ignacio Vazquez-Abrams Avatar answered Nov 12 '22 19:11

Ignacio Vazquez-Abrams