I’m trying to create a dictionary where each value is a list of functions that should get executed when calling the value. Here’s some pseudo-code:
JavaScript
x
5
1
functions = {
2
"A": (execute() for execute in [func1(param1), func2(param1)]),
3
"B": (execute() for execute in [func1(param2), func2(param2)]),
4
}
5
But if I try to call this using functions["A"]()
an exception is thrown saying “TypeError: ‘generator’ object is not callable”.
How should I approach this problem?
EDIT: Just to clarify, the functions are not generators nor have any return type. They just execute some actions based on the parameter.
Advertisement
Answer
ACCEPTED ANSWER:
Thanks to all of your suggestions, I came up with a solution. I’m just going to leave the code here:
JavaScript
1
23
23
1
def move(params): # params is a list of 5 parameters of different types
2
# Execute move
3
4
def rotate(params): # params is a list of 2 parameters of different types
5
# Execute rotation
6
7
8
moves: {
9
"A": [(move, [param1, param2 etc ]), (rotate, [other_param1, other_param2])
10
"B": [(move, [param3, param4 etc ]), (rotate, [other_param3, other_param4])
11
}
12
13
14
def execute_move(move):
15
for func, params in moves[move]:
16
func(params)
17
18
19
# Example use
20
execute_move("A")
21
execute_move("B")
22
execute_move("A")
23
This works really well, so thank you all for your help.