I have a list of functions, like so:
def foo(a, b):# do stuffreturn True # or Falsedef bar(c):# do stuffreturn False # or Truedef baz(d, e, f):# do stuffreturn True # or False
and I want to call each of them in turn, and only proceed to the next one if the previous one returns True
, like so:
if foo(1, 2):if bar(3):if baz(4, 5, 6):print("Success!")
Now, I know that if all of my functions accepted the same arguments, I could do something like:
steps = [foo, bar, baz]
if all(func(*args) for func in steps):print("Success!")
because all
would short-circuit as soon as it reaches a False
return value. However, they do not all accept the same arguments.
What would the cleanest way to achieve this?
Edit: Thanks for the suggestions about zipping the args. What about a more general case where each function returns some value, and I want to use the return value of the previous function, whatever it may be, to decide whether or not I run the next function? Each function may also require as arguments some of the return values of the previous functions.