Catherine M Moroney <catherine.m.moro...@jpl.nasa.gov> Wrote in
 message:
> Hello,
> 
> Pardon me for not using the proper Python language terms, but I hope 
> that people can still understand the question:
> 
> The problem:  I'm writing a large Python program and I have a bunch of
> parameters (whose values are static) that I want to make available to
> the rest of the code with minimum overhead and duplicate processing.
> 
> I think that the simplest way would be to create a file called 
> "Params.py" and then simply have statements like a = 1, b = 2, etc.
> in there (no classes, no methods, just a bunch of declarations).  But, 
> some of these static parameters have to be calculated rather than simply 
> hard-coded.
> 
> I thought of creating a class called Params and having a bunch of
> methods (decorated with @classmethod) that set/calculate the value of
> all the parameters.  Easy enough, but then I have to create a Params
> object in every source file that uses these parameters, and that seems
> wasteful.

No, you don't want to create separate instances of the Params
 class.  You want to create exactly one object,  and have each
 source file get their attributes from the same object.

That object could be the same module object as already mentioned.
 Or it could be an object created in the module exactly once. I
 like the latter method and I'll explain why after showing
 how.

In parametersetup.py, you do something like:

class Foo:  # assuming python 3.x
    def __init__(self ):
        self.parm1 = 42
        self.otherparm =3


parms = Foo ()  # this is the sole instance and its only created here.
del Foo             # in case you want to make sure.

In each source file you have something like:

from parametersetup import parms

and you refer to parms.param1


Now, why the extra fuss? In case you later want a different set of
 parms for each thread, or for certain modules, or for debugging. 
 You're halfway there.



-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to