【发布时间】:2012-05-23 09:16:59
【问题描述】:
我正在尝试做“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)做了什么(?)
如果有人能向我解释上述两行,我将不胜感激。感谢您的宝贵时间。
【问题讨论】:
标签: python python-2.7