classes vs dicts
John Hunter
jdhunter at ace.bsd.uchicago.edu
Thu May 6 23:01:43 EDT 2004
>>>>> "Charlie" == Charlie <charlvj at yahoo.com> writes:
Charlie> Greetings, I am pretty new to Python and like it very
Charlie> much, but there is one thing I can't figure out and I
Charlie> couldn't really find anything in the docs that addresses
Charlie> this.
Charlie> Say I want to write an address book program, what is the
Charlie> best way to define a person (and the like): create a
Charlie> class (as I would do in Java) or use a dictionary? I
Charlie> guess using dictionaries is fastest and easiest, but is
Charlie> this recommended?
For simple data with just a few fields, a tuple may be all you need
people = (
('John', 'D', 'Hunter', 36),
('Miriam', 'A', 'Sierig', 33),
('Rahel', 'S', 'Hunter', 6),
)
for first, middle, last, age in people:
print '%s %s %s is %d years old' % (first, middle, last, age)
When you use named tuple unpacking, ie,
first, middle, last, age = row
rather than indexing operations, ie row[0], I find using tuples can be
as readable as classes or dicts. Again only for simple data
structures...
John Hunter
More information about the Python-list
mailing list