Re: classes vs dicts

From: John Hunter (jdhunter_at_ace.bsd.uchicago.edu)
Date: 05/07/04


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



Relevant Pages

  • Re: What to converse about next?
    ... that this was so in TOS as well, and the skirt was just really fashionable with the ladies at the time. ... SF'er in the shirt/pants rather than the minidress: The junior Yeoman to whom Janice introduced Charlie... ... but that was probably due to the fact that immediately after the introduction, Charlie would bump her age up near the triple-digits, and it was probably considered unpalatable to display a woman of that age in such a costume. ...
    (rec.arts.startrek.tech)
  • OT (Sort of): Happy Birthday David Nelson and...
    ... Charlie Wyman... ... Both men turn 69 years young today! ... (A good age, a great number... ... Prev by Date: ...
    (rec.music.beatles)
  • Re: Ortrudo,io tamo
    ... cuore over cullo, please. ... age, sorry Charlie. ...
    (rec.music.opera)
  • Re: Should I buy this?????????????
    ... those who sincerely,but firmly, have had enough of my responses to ... that "calling" at age 12...and so in the opera world..i try to do the ... I do not want to suddenly sound like "Papa Charlie," ...
    (rec.music.opera)
  • Re: Power 2000 Lith battery problem
    ... and your battery is a lot younger than ... So whilst it's unlikely that age is the problem, ... Whiskey Echo Sierra Sierra AT Alpha Charlie Echo Golf Romeo Oscar Paul dot Charlie Charlie ...
    (rec.photo.digital)