classes vs dicts

Larry Bates lbates at swamisoft.com
Thu May 6 16:03:45 EDT 2004


You will probably want a dictionary with keys to
find people with classes stored to hold the information
about the people.  That way you can extend the class
easily without disrupting your data structures.

class person:
    def __init__(self, lastname, firstname, initial):
    self.lastname=lastname
    self.firstname=firstname
    self.initial=initial
    return

#
# Main Program
#
all_people={}

all_people['LarryABates']=person('Larry','Bates','A')
all_people['SomeOPerson']=person('Some','Person','O')

Then you can do things like:

all_people['LarryABates'].telephone="2055551234"
all_people['LarryABates'].faxphone="2055551235"

or

entry=all_people['LarryABates']

print entry.lastname
print entry.firstname
print entry.initial
print entry.telephone
entry.emailaddress="name at domain.com"

I was new to OOP and it took me the longest to understand
that I can store classes just like any other data value.

HTH,
Larry Bates
Syscon, Inc.

"Charlie" <charlvj at yahoo.com> wrote in message
news:be05f1d.0405060212.43929fe1 at posting.google.com...
> Greetings,
>
> I am pretty new to Python and like it very much, but there is one
> thing I can't figure out and I couldn't really find anything in the
> docs that addresses this.
>
> Say I want to write an address book program, what is the best way to
> define a person (and the like): create a class (as I would do in Java)
> or use a dictionary?
> I guess using dictionaries is fastest and easiest, but is this
> recommended?
>
> Thanx for any help.





More information about the Python-list mailing list