Objects versus dictionaries

Chris Rebert clp2 at rebertia.com
Sun Nov 14 14:20:48 EST 2010


On Sun, Nov 14, 2010 at 11:00 AM, Micah Carrick <micah at greentackle.com> wrote:
> I'm writing a little API that other people will use. There are up to 3
> "objects" that get passed around. One of them has some validation methods,
> the other two simply store data and probably won't have any validation or
> other methods. I only made them objects so that they are syntactically (is
> that a word?) similar the other object rather than using dictionaries. I
> figure it also better allows for changes in the future.
>
> Any thoughts on the pros/cons of using my own objects over a dictionary
> objects?

Objects are definitely nicer to work with syntactically, and they help
make your program's types more explicit.
Rather than coding the "data holder" classes manually, consider using
namedtuples instead:
http://docs.python.org/library/collections.html#collections.namedtuple

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list