Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass kwargs with invalid key=value pairs to function

Tags:

python

The following code:

def f(a=1):
    pass

kwargs = {}
kwargs['a'] = 1
kwargs['b'] = 2

f(**kwargs)

(correctly) raises an Exception:

Traceback (most recent call last):
  File "tt.py", line 8, in <module>
    f(**kwargs)
TypeError: f() got an unexpected keyword argument 'b'

Is there a way, with functools or other, to get around this and figure out which arguments didn't get used by the function in order to be able to pass them to another function? For example, I might have another function:

def g(a=None, b=None):
    pass

which I want to call after, with e.g.

g(**kwargs)

but I only want b to be passed because a was already 'used up' in the previous function.

Now I know that this is not ideal coding, but there are cases where it can come in handy, and it is actually easy to explain to the user, e.g. "Additional parameters will be passed to f, and any parameters not passed to f will be passed to g".

like image 206
astrofrog Avatar asked Aug 27 '11 20:08

astrofrog


People also ask

How do you send Kwargs to functions?

Kwargs allow you to pass keyword arguments to a function. They are used when you are not sure of the number of keyword arguments that will be passed in the function. Kwargs can be used for unpacking dictionary key, value pairs. This is done using the double asterisk notation ( ** ).

How do you pass Kwargs in Python class?

Python **kwargs For this problem Python has got a solution called **kwargs , it allows us to pass the variable length of keyword arguments to the function. In the function, we use the double asterisk ** before the parameter name to denote this type of argument.

What is the correct way to use the Kwargs statement?

The double asterisk form of **kwargs is used to pass a keyworded, variable-length argument dictionary to a function. Again, the two asterisks ( ** ) are the important element here, as the word kwargs is conventionally used, though not enforced by the language.

Can you have Kwargs without args?

Python is pretty flexible in terms of how arguments are passed to a function. The *args and **kwargs make it easier and cleaner to handle arguments. The important parts are “*” and “**”. You can use any word instead of args and kwargs but it is the common practice to use the words args and kwargs.


1 Answers

I am a bit astonished that you ask this question and fear that you are doing something you may regret.

Are you trying to call different methods with the same dictionary providing all arguments combined? If so, then the complexity required to handle this case shouldn't be dealt with in the called methods but the calling code, e.g. by tailoring the kwargs to the specific method to be called:

def f(a=1):
    pass

call_tailored_args(f, kwargs)

The helper function would be implemented like this:

import inspect

def tailored_args(f, kwargs):
    relevant_args = {k: v in kwargs if k in inspect.getargspec(f).args}
    f(**relevant_args)
like image 82
blubb Avatar answered Nov 08 '22 06:11

blubb