"""
Using dict to implement dispatch
"""
animals = []
number_of_felines = 0
def deal_with_a_cat():
global number_of_felines
print " meow "
animals.append( ' feline ' )
number_of_felines += 1
def deal_with_a_dog():
print " bark "
animals.append( ' canine ' )
def deal_with_a_bear():
print " watch out for the *HUG*! "
animals.append( ' ursine ' )
tokenDict = {
" cat " : deal_with_a_cat,
" dog " : deal_with_a_dog,
" bear " : deal_with_a_bear,
}
words = [ " cat " , " bear " , " cat " , " dog " ]
for word in words:
funcToCall = tokenDict[word]
funcToCall()
# #####################################
the output is :
>>>
meow
watch out for the * HUG * !
meow
bark
>>>
"""
animals = []
number_of_felines = 0
def deal_with_a_cat():
global number_of_felines
print " meow "
animals.append( ' feline ' )
number_of_felines += 1
def deal_with_a_dog():
print " bark "
animals.append( ' canine ' )
def deal_with_a_bear():
print " watch out for the *HUG*! "
animals.append( ' ursine ' )
tokenDict = {
" cat " : deal_with_a_cat,
" dog " : deal_with_a_dog,
" bear " : deal_with_a_bear,
}
words = [ " cat " , " bear " , " cat " , " dog " ]
for word in words:
funcToCall = tokenDict[word]
funcToCall()
# #####################################
the output is :
>>>
meow
watch out for the * HUG * !
meow
bark
>>>