How to check arguments of the function?

前端 未结 3 1877
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 00:23

I have function defined this way:

def f1 (a, b, c = None, d = None):
    .....

How do I check that a, b are not equal

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-25 00:47

    Why can't you refer to the values by their names?

    def f1 (a, b, c=None, d=None):
        if not a.strip():
            print('a is not empty')
    

    If you have many arguments it is worth changing function signature to:

    def f2 (*args, c=None, d=None):
        for var in args:
            if not var.strip():
                raise ValueError('all elements should be non-empty')
    

提交回复
热议问题