When a function I'm calling has a lot of parameters, and there's one I'd like to include conditionally, do I have to have two separate calls to the function, or is there someway to pass nothing (almost like None
) so that I'm not passing in any argument for a particular parameter?
For example, I want to pass an argument for the parameter sixth
sometimes, but other times I want to not pass anything for that parameter. This code works, but it feels like I'm duplicating more than I should have to.
The function I'm calling is in a third-party library, so I can't change how it handles the received arguments. If I pass in None
for sixth
, the function raises an exception. I need to either pass my 'IMPORTANT_VALUE'
or not pass in anything.
What I'm doing currently:
def do_a_thing(stuff, special=False):
if special:
response = some.library.func(
first=os.environ['first'],
second=stuff['second'],
third=stuff['third']
fourth='Some Value',
fifth=False,
sixth='IMPORTANT_VALUE',
seventh='example',
eighth=True
)
else:
response = some.library.func(
first=os.environ['first'],
second=stuff['second'],
third=stuff['third']
fourth='Some Value',
fifth=False,
seventh='example',
eighth=True
)
return response
What I'd like to do:
def do_a_thing(stuff, special=False):
special_value = 'IMPORTANT_VALUE' if special else EMPTY_VALUE
response = some.library.func(
first=os.environ['first'],
second=stuff['second'],
third=stuff['third']
fourth='Some Value',
fifth=False,
sixth=special_value,
seventh='example',
eighth=True
)
return response