我正在尝试做“Learn Python the hard way”一书中的练习页面:106。示例如下:
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 = raw_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
?是什么_find
?特别是为什么下划线?同样,我不确定是什么city_found = cities['_find'](cities, state)
。我在同一个问题上看到过类似的帖子:
learn python the hard way exercise 40 help
这基本上说cities['_find'] = find_city
将函数 find_city 添加到字典中,但我仍然不明白是什么city_found = cities['_find'](cities, state)
(?)
如果有人可以向我解释上述两行,我将不胜感激。谢谢你的时间。