You just look at two lines from your code:
cities['_find'] = find_citycity_found = cities['find'](cities, state)
Explanation for (1):
As cities is dictionary before you use cities['_find'].
print it...
print(cities)
Output: {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville', 'NY': 'New York', 'OR': 'Portland'}
Now after using cities['_find']=find_city,
print(cities)
Output: {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville', 'NY': 'New York', 'OR': 'Portland', *'_find': <function find_city at 0x01BAB738*>}
Here the last dictionary item is added with key _find and the value is the function find_city.
Explanation for (2):
Now city_found = cities['find'](cities, state)
Now we know that find_city is in the dict at _find, that means we can do work with it. The it can be broken down like this:
Python sees
city_found =and knows we want to make a new variable.It then reads cities and finds that variable, it’s a dict.
Then there’s
['_find']which will index into the cities dict and pull out whatever is at_find.What is at
['_find']is our functionfind_cityso Python then knows it’s got a function, and it does the function call.The parameters cities, state are passed to this function
find_city, and it runs because it’s called.find_citythen tries to look up states inside cities, and returns what it finds.Python takes what
find_cityreturned, and finally that is what is assigned tocity_found.