def f1(n):
pass
def f2():
pass
FUNCTION_LIST = [(f1,(2)),
(f1,(6)),
(f2,())]
for f, arg in FUNCTION_LIST:
f(arg)
For the third time in the loop, it tries to pass an empty set of arguments to a function that takes no arguments. He gives an error TypeError: f2() takes no arguments (1 given). The first two function calls work correctly - the contents of the tuple are passed, not the tuple itself.
Getting rid of an empty tuple of arguments in an abusive list entry does not solve the problem:
FUNCTION_LIST[2] = (f2,)
for f,arg in FUNCTION_LIST:
f(arg)
leads to ValueError: need more than 1 value to unpack.
I also tried iterating over the index, not the list items.
for n in range(len(FUNCTION_LIST)):
FUNCTION_LIST[n][0](FUNCTION_LIST[n][1])
This gives the same TypeErrorin the first case, and IndexError: tuple index out of rangewhen the third element of the list (f2,).
Finally, asterisk notation doesn't work either. This time it is an error when called f1:
for f,args in FUNCTION_LIST:
f(*args)
gives TypeError: f1() argument after * must be a sequence, not int.
, . , . - ?