store function in dictionary python code example

Example 1: python call a function from a dictionary

def one():
     print('one hahah')

def two():
    print('two hahah')

def the_count():
    print('I am the count who likes to count')


dictionary_name = {
    'one': one, 'second_function': two, 'tree_yeah_i_know': the_count
}


dictionary_name['one']()

dictionary_name['second_function']()

dictionary_name['tree_yeah_i_know']()

Example 2: 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