cities['_find']=find_city 。。不懂,希望大家解释下。
#-*-coding:utf-8-*-cities={'CA':'San Francisio','MI':'Detroit','FL':'Jackesonville'}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 atention!cities['_find']=find_city
print cities['_find'](cities,'NY')
[解决办法]
捆绑一个函数在字典里,纯属个人喜好,有点c的影子。通俗直白一点应该写个类这样比较清楚。find_city有点多余,cities.get(state, 'Not found.')就得了...
[解决办法]
把函数对象放入字典中,在python中,字典中可以放任意对象的。