[Tutor] Re: Help with classes (Joseph Q.)
Andrei
project5 at redrival.net
Sat Apr 9 20:36:31 CEST 2005
Joseph Quigley wrote on Fri, 08 Apr 2005 16:46:33 -0600:
> Now what are dictionaries and the __name__ really used for? A byte of
> python never got that through my thick skull.
Dictionaries associate a value with a certain key, sort of like a real
world dictionary where you can take a word and look up its
meaning/translation (except that real world dictionaries are alphabetically
sorted and only contain words, while Python dictionaries are not sorted and
can contain lots of things, including numbers and tuples).
It's possible to get by without using them directly (other languages don't
even have something resembling a dictionary and they still manage to work
somehow :)), but you might end up building functionality to emulate their
behaviour with for example lists. Let's say you build an application which
allows the user to attach comments to their pictures: you could use a
dictionary for it, mapping a string to each file name:
comments = {}
while True:
photo = raw_input("Picture name: ")
if photo = '':
break
comment = raw_input("Comment: ")
comments[photo] = comment
print comments
You could also think of an address book (where you use names as keys and
addresses as values), a menu system (where certain user input is mapped to
a certain function), a cache system (where results of certain
time-consuming operations are saved in case the user asks for the same
operation again) and there are many, many other uses.
--
Yours,
Andrei
=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
More information about the Tutor
mailing list