Reputation: 3
I'm learning python from the book called "Learn python the hard way" and i came across this block of code in exercise40
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
1.Is it necessary to add "find_city" function to the dictionary?
2.Can i remove "themap" argument in the function "find_city"?
Here is my new code and it can still run without any error:
cities = {"CA" : "San Francisco", "MI" : "Detroit", "FL" : "Jacksonville"}
cities["NY"]= "New York"
cities["OR"] = "Portland"
def find_city(state):
if state in cities:
return cities[state]
else:
return "Not Found."
while True:
print "State? (ENTER to quit)",
state = raw_input(">")
if not state:
break
city_found = find_city(state)
print city_found
Upvotes: 0
Views: 285
Reputation: 51663
You can do this, but the first 'codepiece' example stores a "functionpointer" for the find-function inside the dictionary itself so you can use the function by using a key of '_find'
and supply the parameters to the found value (the FP) for this key.
I do not know the book, but probably this is done on a purpose, like f.e. to teach you that "values" in a dictionary can be of different types - maybe even an introduction to study functions as objects and how to use them....
In a real world example I would not do this, I might wrap the whole dictionary in a class and supply class methods IF (and thats a big if) there are other concerns that leads me to wrap stuff for this class...
All in all it is a very contreived example (in germany we would say "An den Haaren herbeigezogen" wich roughly translates to "pulled by it's hairs" (word by word) or "pulled out of the a..")
I think you are better of using the python3 website and its examples to learn - there are plenty of things to gain there - and study the documentation.
Start here: https://docs.python.org/3/tutorial/
Upvotes: 1
Reputation: 42758
Yes, it's strange, to put a function in a dictionary with cities. I wouldn't do that, but I don't know, what the author want to demonstrate with.
It is not a good idea, to rely on the global variable cities
in find_city
.
Usually, you would use .get
:
def find_city(mapping, state):
return mapping.get(state, "Not Found.")
while True:
print "State? (ENTER to quit)",
state = raw_input(">")
if not state:
break
city_found = find_city(cities, state)
print city_found
Upvotes: 3
Reputation: 133929
no, it is not.
yes you can.
And as a side note, many of the top python answerers here on Stack Overflow do not recommend using Learn Python the Hard Way - quite contrary. As a newcomer, in 2017, your time would be more well-spent learning Python 3 and using some other resource.
Upvotes: 5