Using self.xxxx as a default parameter - Python

前端 未结 3 700
失恋的感觉
失恋的感觉 2020-11-27 19:54

I\'m trying to simplify one of my homework problems and make the code a little better. What I\'m working with is a binary search tree. Right now I have a function in my

3条回答
  •  渐次进展
    2020-11-27 20:26

    If you want to treat None as a valid argument, you could use a **kwarg parameter.

    def function(arg1, arg2, **kwargs):
        kwargs.setdefault('arg3', default)
        arg3 = kwargs['arg3']
    
        # Continue with function
    
    function("amazing", "fantastic") # uses default
    function("foo", "bar", arg3=None) # Not default, but None
    function("hello", "world", arg3="!!!")
    

提交回复
热议问题