cities = {'CA': 'San Francisco', 'MI': 'Detroit',
'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."
# ok pay attention!
cities['_find'] = find_city
while True:
print ("State? (ENTER to quit)"),
state = input("> ")
if not state: break
# this line is the most important ever! Study!
city_found = cities['_find'](cities, state)
print (city_found)
cities[‘_find’] = find_city這里的cities[‘_find’](是創(chuàng)建的字典中的一個元素)被賦值成find__city函數哈蝇,也可以理解為cities[‘_find’]是一個函數變量词爬。