Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to deal with sometimes unused arguments of function?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading