How to check whether optional function parameter is set

前端 未结 10 1354
傲寒
傲寒 2020-12-02 15:06

Is there an easy way in Python to check whether the value of an optional parameter comes from its default value, or because the user has set it explicitly at the function ca

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 15:28

    I agree with Volatility's comment. But you could check in the following manner:

    def function(arg1,...,**optional):
        if 'optional_arg' in optional:
            # user has set 'optional_arg'
        else:
            # user has not set 'optional_arg'
            optional['optional_arg'] = optional_arg_default_value # set default
    

提交回复
热议问题