hey guys i am having trouble understanding this, i dont get when themap is referenced to the cities dict really. or the last line, what is the(cities, state) part?
thanks.
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']is exactlyfind_city. Socities['_find'](cities, state)is the same asfind_city(cities, state).The reason for my first statement is this line:
That doesn’t call
find_city, it sticks the function itself in the dictionary. Python functions are just objects like lists and class instances. If you don’t put parentheses after them, they can be assigned to variables.