Conditionally passing arbitrary number of default named arguments to a function

前端 未结 4 1812
情书的邮戳
情书的邮戳 2020-12-18 23:58

Is it possible to pass arbitrary number of named default arguments to a Python function conditionally ?

For eg. there\'s a function:

def func(arg, ar         


        
4条回答
  •  轮回少年
    2020-12-19 00:30

    That wouldn't be valid Python syntax you have to have something after else. What is done normally is:

    func('arg', 'arg2', 'some value' if condition else None)
    

    and function definition is changed accordingly:

    def func(arg, arg2='', arg3=None):
        arg3 = 'def' if arg3 is None else arg3
    

提交回复
热议问题