Class or Dictionary?
tjreedy at udel.edu
Fri Feb 11 19:32:29 CET 2011
On 2/11/2011 9:56 AM, Martin De Kauwe wrote:
> 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.
Dicts are, of course objects. That said, if all keys are identifier
strings, then attribute access is nicer. A module is essentially a dict
with all identifier keys, accessed as attributes.
mod.a is mod.__dict__['a']
Module are sometimes regarded as singleton data-only classes.
Just put 'import params' in every file that needs to read or write them.
The stdlib has at least a few modules that consist only of constants.
One example is tkinter.constants.
Terry Jan Reedy
More information about the Python-list