Pythonic solution for conditional arguments passing

后端 未结 9 784
无人共我
无人共我 2020-12-29 20:24

I have a function with two optional parameters:

def func(a=0, b=10):
    return a+b

Somewhere else in my code I am doing some conditional a

9条回答
  •  春和景丽
    2020-12-29 20:52

    Going by the now-deleted comments to the question that the check is meant to be for the variables being None rather than being falsey, change func so that it handles the arguments being None:

    def func(a=None, b=None):
       if a is None:
          a = 0
       if b is None:
          b = 10
    

    And then just call func(a, b) every time.

提交回复
热议问题