Class or Dictionary?

Dan Stromberg drsalists at
Fri Feb 11 13:15:27 EST 2011

On Fri, Feb 11, 2011 at 6:56 AM, Martin De Kauwe <mdekauwe at> wrote:
> Hi,
> I have a series of parameter values which i need to pass throughout my
> code (>100), in C I would use a structure for example. However in
> python it is not clear to me if it would be better to use a dictionary
> or build a class object? Personally I think accessing the values is
> neater (visually) with an object rather than a dictionary, e.g.
> x = params['price_of_cats'] * params['price_of_elephants']
> vs.
> x = params.price_of_cats * params.price_of_elephants
> So currently I am building a series of class objects to hold different
> parameters and then passing these through my code, e.g.
> class EmptyObject:
>    pass
> self.animal_prices = EmptyObject()
> self.price_of_cats = 12 or reading a file and populating the object
> I would be keen to hear any reasons why this is a bad approach (if it
> is, I haven't managed to work this out)? Or perhaps there is a better
> one?

I'd use a class rather than a dictionary - because with a class,
pylint (and perhaps PyChecker and pyflakes?) should be able to detect
typos upfront.  With a dictionary, typos remain runtime timebombs.

More information about the Python-list mailing list