I have a Python function accepting several string arguments def foo(a, b, c): and concatenating them in a string.
I want to iterate over all function arguments to check they are not None. How it can be done?
Is there a quick way to convert None to “”?
Thanks.
locals()may be your friend here if you call it first thing in your function.Example 1:
Example 2:
Answer:
Update:
‘Example 1’ highlights that we may have some extra work to do if the order of your arguments is important as the
dictreturned bylocals()(orvars()) is unordered. The function above also doesn’t deal with numbers very gracefully. So here are a couple of refinements: