当我调用的函数有很多参数,并且我想有条件地包含一个参数时,我是必须对该函数进行两个单独的调用,还是有什么方法可以不传递任何参数(几乎类似于None),这样我就不会为某个特定参数传入任何参数了?在
例如,有时我想为参数sixth传递一个参数,但有时我希望而不是为该参数传递任何参数。这段代码很好用,但感觉我复制的东西太多了。在
我调用的函数位于第三方库中,因此我无法更改它处理接收到的参数的方式。如果我为sixth传入None,函数将引发异常。我需要传递我的'IMPORTANT_VALUE'或者不传递任何内容。在
我目前在做什么: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
我想做的是:
^{pr2}$