I have a routine that will take a list of tuples in the form:
(function, [optional] arguments, [optional] keyword arguments)
as part of a Command design pattern implementation.
ORG1_PROCESS = [(add_header_row, df, COLUMN_LABELS['ORG1']),
(func_1, {'abc' = 123}),
(func_2), ]
ORG2_PROCESS = [(add_header_row, df, COLUMN_LABELS['ORG2']), ]
def process_commands(a_list: List[Tuple[Any, ...]]) -> None:
for item in a_list:
(func, *args, **kwargs) = item
if kwargs:
func(*args, **kwargs)
elif args:
func(*args)
else:
func()
I want to execute like this:
process_commands(ORG1_PROCESS)
to execute the equivalent of:
add_header_row(df, COLUMN_LABELS['ORG1'])
func_1(abc=123)
func_2()
However, I have a syntax error:
(func, *args, **kwargs) = item
^
SyntaxError: invalid syntax
I thought that is valid tuple unpacking in Python 3.9.7. Any advice on what am I missing here would be great!