My previous answer was not generic enough, so here's another bash at it. This uses inspect.getargspec() to determine the arity of each function prior to calling it. With that knowledge the required number of arguments can be sliced out of a "results list" which contains the results of all previous function calls.
There are some limitations:
inspect.getargspec() doesn't work for built-in functions, only user defined ones.
- varargs and keyword args are ignored because this results in possibly infinite arguments which just complicates things.
Code:
import inspect
def a():
return 25
def b():
return 50
def c(x, y):
return x + y
def d(x, y, z):
return x + y + z
def e(x=100):
return x * x
def f():
pass
def execute_function_list(funcs):
results = []
for i, fn in enumerate(funcs):
args_required = len(inspect.getargspec(fn).args)
if args_required > 0:
args = results[i-args_required:i]
else:
args = ()
print("calling function %r with args %r" % (fn, args))
results.append(fn(*args))
print("after function returned results = %r" % results)
return results[-1] if len(results) else None
if __name__ == '__main__':
funcs = [e, a, b, c, d, e]
print("final result is %r" % execute_function_list(funcs))
l[2](l[0](), l[1]())def c(x=a(), y=b()):