Class or Dictionary?
Martin De Kauwe
mdekauwe at gmail.com
Sun Feb 13 19:12:00 EST 2011
On Feb 14, 10:16 am, Martin De Kauwe <mdeka... at gmail.com> wrote:
> On Feb 13, 6:35 pm, Terry Reedy <tjre... at udel.edu> wrote:
> > On 2/12/2011 9:20 PM, Martin De Kauwe wrote:
> > > On Feb 13, 5:12 am, Terry Reedy<tjre... at udel.edu> wrote:
> > >> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:
> > >>> The point of this posting was just to ask those that know, whether it
> > >>> was a bad idea to use the class object in the way I had or was that
> > >>> OK? And if I should have just used a dictionary, why?
> > >> Did you miss my suggestion to use a module rather than a class?
> > >> Modules do not have some of the disadvantages of classes listed by J. Nagle.
> > >> --
> > >> Terry Jan Reedy
> > > Hi, sorry I did. I just re-read it, could you provide an example?
> > I am not sure what you are asking. tkinter.contants is one example.
> > > Sorry i am having a major mind blank this morning (I think this is
> > > obvious!?). And it would meet all of the criteria outlined by John
> > > Nagle?
> > A module will work fine if but only if you commit yourself to having all
> > keys be legal Python identifiers. But that is usually not a problem with
> > configuration values.
> > --
> > Terry Jan Reedy
> I think I got it, did you mean something like this?
> class Constants:
> radius_of_earth = 6.37122E+6
> days_as_yrs = 1.0 / 365.25
> m2_as_ha = 1E-4 # metres squared as hectares
> g_as_tonnes = 1E-6 # grammes as tonnes
> kg_as_tonnes = 1E-3 # kg as tonnes
> kg_as_g = 1E+3
> def __init__(self):
> self.radius_of_earth = self.__class__.radius_of_earth
> self.days_as_yrs = self.__class__.days_as_yrs
> self.m2_as_ha = self.__class__.m2_as_ha
> self.g_as_tonnes = self.__class__.g_as_tonnes
> self.kg_as_tonnes = self.__class__.kg_as_tonnes
> self.kg_as_g = self.__class__.kg_as_g
> usage something like
> >>>from constants import Constants
> Something similar for the params?
And then I guess define some default parameters and then use configobj
to parse a .INI file. Then adjust those parameters change in the .INI
from parameters import DefaultParameters as params
# d -> comes from .INI file
p = params()
for key, value in d.iteritems():
# change anything read from file
p.__dict__[key] = value
More information about the Python-list