[Tutor] dict['_find']
Max Niederhofer
max at niecap.com
Sun Feb 20 14:30:40 CET 2011
Steven, Alan, Knacktus,
thanks for your help. I was indeed very confused because I thought
'_find' was calling something special instead of just being added to
the dictionary (the confusion stemming from
http://www.python.org/dev/peps/pep-0004/ where find module is
obsolete).
When I ran the code, the error I got was:
max:python max$ python ex40.py
State? (ENTER to quit) > CA
Traceback (most recent call last):
File "ex40.py", line 22, in <module>
city_found = cities['_find'](cities, state)
File "ex40.py", line 8, in find_city
return themap(state)
TypeError: 'dict' object is not callable
Two main mistakes: (1) not including the actual search in the while
loop and (2) wrong parentheses in return themap(state) instead of
return themap[state].
Fixed version which runs:
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."
cities['_find'] = find_city
while True:
print "State? (ENTER to quit)",
state = raw_input ("> ")
if not state: break
city_found = cities['_find'](cities, state)
print city_found
Thanks for your help, especially the comments about keeping things
separate and explicit. Now to find out why Zed Shaw thinks this is a
good way of doing it...
Best,
Max
On Sun, Feb 20, 2011 at 9:40 AM, Knacktus <knacktus at googlemail.com> wrote:
> Am 20.02.2011 05:14, schrieb Max Niederhofer:
>
>> Hello all,
>
> Hello Max,
>>
>> first post, please be gentle. I'm having serious trouble finding an
>> alternative for the deprecated find module for dictionaries.
>>
>> The code (from Zed Shaw's Hard Way, exercise 40) goes something like
>> this. Hope indentation survives.
>>
>> cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
>
> I use a naming convention for dicts that has made me very happy on several
> occasion ;-):
> key_to_value, in your case
> state_to_city = {...}
>>
>> def find_city(themap, state):
>> if state in themap:
>> return themap[state]
>> else:
>> return "Not found."
>>
>> cities['_find'] = find_city
>
> Did you put this entry into the same dictionary as the data on purpose?
> Or is the purpose a kind of dispatch? Something that could be a dict on its
> own, like
> private_function_name_to_function = {'_find': find_city}
> You should try to keep things seperate and explicit.
>>
>> while True:
>> print "State? (ENTER to quit)",
>> state = raw_input("> ")
>>
>> if not state: break
>>
>> city_found = cities['_find'](cities, state)
>> print city_found
>>
>> My question is - how do I rewrite this using an alternate module given
>> find is deprecated? Grateful for all suggestions or pointers. For
>> reference, I'm using 2.6.1 on darwin.
>>
>> Thanks so much for your help.
>>
>> Best,
>> Max
>>
>> --
>> Dr. Maximilian Niederhofer
>> Founder, Qwerly
>> http://qwerly.com/ | http://qwerly.com/max
>> +44 78 3783 8227
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Dr. Maximilian Niederhofer
Founder, Qwerly
http://qwerly.com/ | http://qwerly.com/max
+44 78 3783 8227
More information about the Tutor
mailing list