只是给出一个使用词典的例子(就像在其他答案中提到的那样),如果你觉得有用的话.
def get_list1():
print('get_list1 executes')
def get_list2():
print('get_list2 executes')
# Create a dictionary with a reference to your functions as values
# (note no parenthesis, as that would execute the function here instead)
fns = {
'example_key1': get_list1,
'example_key2': get_list2,
}
print(type(fns['example_key1'])) # returns <class 'function'>
# If you still want a list
lst = list(fns) # Create a list containing the keys of the fns dictionary
for fn in lst:
# Iterate through the list (of keys) and execute the function
# found in the value.
fns[fn]()
# Or you can now just simply iterate through the dictionary instead, if you wish:
for fn in fns.values():
fn()
此代码生成:
<class 'function'>
get_list1 executes
get_list2 executes
get_list1 executes
get_list2 executes