I have a part of code in python, which calls dynamically different functions, where I always want to pass 3 different arguments. However, these functions, might not always need to use those 3 different arguments. Here is a very simple code that shows the issue:
def test_keyword_args():
def fn1(a, b, c):
return a + b
def fn2(a, b, c):
return a + c
def fn3(a, b, c):
return b + c
obj = {
'a': fn1,
'b': fn2,
'c': fn3,
}
for key in obj:
value = obj[key](a=1, b=2, c=3)
if key == 'a':
assert value == 3
if key == 'b':
assert value == 4
if key == 'c':
assert value == 5
How can I always call same function obj[key](a=1,b=2,c=3)
passing this keyword arguments, and avoid complains about unused parameters? (c not used in fn1, b not used in fn2, a not used in fn3)
I can imagine suppressing warnings would do the trick, but I do not think it is the appropriate solution
I am using Python 3.7.3
You can define arguments as keyword only by prefixing the argument list with *
, you can then avoid the unused parameter warnings by naming a parameter _
. Using **_
allows us to ignore any keyword arguments not in our named parameters
def fn1(*, a, b, **_):
return a + b
def fn2(*, a, c, **_):
return a + c
def fn3(*, b, c, **_):
return b + c