For instance, I’ve tried things like this, which doesn’t work:
mydict = {
'funcList1': [foo(), bar(), goo()],
'funcList2': [foo(), goo(), bar()]}
Is there some kind of structure with this kind of functionality?
I realize that I could obviously do this just as easily with a bunch of def statements:
def func1():
foo()
bar()
goo()
But the number of statements I need is getting pretty unwieldy and tough to remember. It would be nice to wrap them nicely in a dictionary that I could examine the keys of now and again.
Functions are first class objects in Python and so you can dispatch using a dictionary. For example, if
fooandbarare functions, anddispatcheris a dictionary like so.Note that the values are
fooandbarwhich are the function objects, and NOTfoo()andbar().To call
foo, you can just dodispatcher['foo']()EDIT: If you want to run multiple functions stored in a list, you can possibly do something like this.