Pythonic solution for conditional arguments passing

后端 未结 9 781
无人共我
无人共我 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:54

    Why not pass that logic to the function?

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

提交回复
热议问题