how to store a function in a dictionary python code example
Example: how to store a function in a dictionary python
# Functions are first class objects in Python and so you can dispatch using a dictionary.
# For example, if foo and bar are functions, and dispatcher is a dictionary like so.
def foo():
print('nice')
def bar():
print('drink')
>>> dispatcher = {'foo': foo, 'bar': bar}
# Note that the values are foo and bar which are the function objects, and NOT foo() and bar().
>>> dispatcher['foo']()
nice