How to deal with sometimes unused arguments of function?

237 views Asked by At

Could you please advise me how to deal with sometimes unused arguments of function? Thank you in advance!

Function:

def foo(a, b=2, c="default"):
  print(c)
  return a + b

Usage:

arg_1 = int(input())
if arg_1 == 2:
  arg_2 = 3
  arg_3 = "using 3"
else:
  # what can I do here?
  arg_2 = *universal_value_that_is_disabling_argument
  arg_3 = *universal_value_that_is_disabling_argument
  # I know that I can use arg_2 = 2 and arg_3 = "default", 
  # but it is not really convenient when there are many arguments

foo(arg_1, b=arg_2, c=arg_3)

I understand that I can do something like this, but it is not really convenient when there are many arguments:

arg_1 = int(input())
if arg_1 == 2:
  foo(arg_1, 3, "using 3")
else:
  foo(arg_1)
1

There are 1 answers

1
Barmar On BEST ANSWER

Call it by unpacking a dictionary for the named arguments. Then you can simply omit the arguments that should get the default.

if arg_1 == 2:
    options = {'b': 3, 'c': 'using 3'}
else:
    options = {}

foo(arg_1, **options)