classes vs dicts

Terry Reedy tjreedy at udel.edu
Thu May 6 10:14:12 EDT 2004


"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?

You can represent a person by a tuple, dictionary, or class instance.  All
three ways of representing records are used in various places in the
standard library.  The 'best way' depends on the specifics of the
situation.

You can represent a collection of persons -- an address book -- by a list,
dictionary, list + index dictionary, dictionary as person class attribute,
class derived from dict, or class with dict as per instance attribute.
Same comment as to 'best'.

> I guess using dictionaries is fastest and easiest, but is this
recommended?

I would concentrate on easiest to program to get desired functionality.

Terry J. Reedy







More information about the Python-list mailing list