I have an object that takes in a function name func, args, and kwargs, and at some point runs
func(*args, **kwargs)
The issue is, if func requires no args/kwargs, args/kwargs default to None, which leads to a TypeError. For example, if the function required no parameters, args, kwargs default to None:
def test():
pass
args = None
kwargs = None
test(*args, **kwargs)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-b9618694fbf2> in <module>()
----> 1 test(*args, **kwargs)
TypeError: test() argument after ** must be a mapping, not NoneType
I'm sure there's a good way to solve this without cascading if statements checking if args/kwargs exist, each with its own function call, but I'm not sure how. The main goal is to pass a function, and its unknown parameters to an object, which will then use them in a method later.
Edited: added an example for clarity
Noneif no arguments are provided when calling the function. Are you referring to a specific keyword argument instead? If so, you can always dokwargs.get('myarg', 'default_value')? - Vlad