I have a Python function in which I am doing some sanitisation of the input parameters:
def func(param1, param2, param3): param1 = param1 or '' param2 = param2 or '' param3 = param3 or ''
This caters for the arguments being passed as None rather than empty strings. Is there an easier/more concise way to loop round the function parameters to apply such an expression to all of them. My actual function has nine parameters.
This looks like a good job for a decorator. How about this:
You would use this on your function like so:
Then the parameters will be replaced by the empty string if they are false:
(Note that this will still mess up the function signature as Ned Batchelder points out in his answer. To fix that you could use Michele Simionato’s decorator module— I think you’d just need to add a
@decoratorbefore the definition ofsanitized)