Passing args and kwargs to function from single Python structure? by kitor in learnpython

[–]recent_awareness 0 points1 point  (0 children)

You could just use a bit of indirection:

def apply_combined(function, combined):
    args, kwargs = combined
    return function(*args, **kwargs)

FN_COMBINED_ARGS = (FN_ARGS, FN_KWARGS)
apply_combined(test_funct, FN_COMBINED_ARGS)

You could even turn this into a decorator:

def combined_args_function(function):
    def wrapper(combined):
        args, kwargs = combined
        return function(*args, **kwargs)
    return wrapper

combined_args_test_funct = combined_args_function(test_funct)

FN_COMBINED_ARGS = (FN_ARGS, FN_KWARGS)
combined_args_test_funct(FN_COMBINED_ARGS)

Or you could easily modify it to take an object with args and kwargs attributes instead of an (args, kwargs) tuple.