classes vs dicts

Richard Taylor rjt-usenet at thegrindstone.me.uk
Thu May 6 06:30:01 EDT 2004


Charlie

Personally I would almost always use a class. 

A class is much more flexible and you may want to add some methods to you
people.

A class also allows you to put format checking into the constructor, define
getters/setters etc.

For example:

        class Person(object):
            def __init__(self,name="unknown"):
                self.name = name

            # If you like setters and getters.
            def getName(self): return self.__name
            def setName(self, value): self.__name = value
            def delName(self): del self.__name
            name = property(getName, setName, delName, "I'm the 'Name' property.")

Richard


Charlie wrote:

> 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