Reputation: 203
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
Upvotes: 2
Views: 1354
Reputation: 50971
cities['_find']
is exactly find_city
. So cities['_find'](cities, state)
is the same as find_city(cities, state)
.
The reason for my first statement is this line:
cities['_find'] = find_city
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.
Upvotes: 9