Re: classes vs dicts
From: John Hunter (jdhunter_at_ace.bsd.uchicago.edu)
Date: 05/07/04
- Next message: Bryan Castillo: "Re: polymorphism w/out signatures?"
- Previous message: David M. Cooke: "Re: MySQL vrs SQLite"
- In reply to: Charlie: "classes vs dicts"
- Next in thread: David MacQuigg: "Re: classes vs dicts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 06 May 2004 22:01:43 -0500 To: charlvj@yahoo.com (Charlie)
>>>>> "Charlie" == Charlie <charlvj@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
- Next message: Bryan Castillo: "Re: polymorphism w/out signatures?"
- Previous message: David M. Cooke: "Re: MySQL vrs SQLite"
- In reply to: Charlie: "classes vs dicts"
- Next in thread: David MacQuigg: "Re: classes vs dicts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|