Q:

Python—Executing All Permutations of List of Functions

I am looking to execute all permutations of a list of functions, both partial and full, and add the results and put them in a list.
For example, say I have three functions:
foo():
 return 1

bar():
 return 2

bat():
 return 3

I want to execute foo(), bar(), bat(), foo() bar(), foo() bat() bar() bat(), and foo() bar() bat().
Therefore, the resulting list would be: [1, 2, 3, 3, 4, 5, 6].
Any idea how I could call all these functions? In reality I will have around 50 functions and want to record all combinations of all functions.
Thank you for any help you give.

A:

Did you know that you could store functions in lists in Python?
You can define all your functions first:
def foo():
    return 1

def bar():
    return 2

def bat():
    return 3

Then, create a list to hold all of them:
list_of_functions = [foo, bar, bat]

Now you can use itertools.combinations to do what you want:
from itertools import combinations

res_list = []

for i in range(len(list_of_functions)):
    comb_list = list(combinations(list_of_functions, i+1))
    for combination in comb_list:
        x = sum(e() for e in combination)
        res_list.append(x)

print(res_list)

