<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jul 14, 2014 at 3:24 PM, Catherine M Moroney <span dir="ltr"><<a href="mailto:Catherine.M.Moroney@jpl.nasa.gov" target="_blank">Catherine.M.Moroney@jpl.nasa.gov</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hello,<br>
<br>
Pardon me for not using the proper Python language terms, but I hope that people can still understand the question:<br>
<br>
The problem: I'm writing a large Python program and I have a bunch of<br>
parameters (whose values are static) that I want to make available to<br>
the rest of the code with minimum overhead and duplicate processing.<br>
<br>
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.<br>
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.<br></blockquote><div><br></div><div>Within a module (such as "Params.py"), you can put any statements or code you want, without needing classes or functions. Depending on the complexity of the calculations, you may want to put some of the functionality into functions, which could then be run on import (just put the function call at the bottom of the module file).</div>
<div><br></div><div>The interpreter will take care to only import one copy, with the exception of if the Params.py file is accessible from multiple paths in sys.path (generally not an issue).</div><div><br></div><div>For example, the following is a valid module (untested):</div>
<div># Params.py</div><div>a = 1</div><div>b = 5</div><div>c = a * b + 42</div><div>d = c - b</div><div># End Params.Py</div><div><br></div><div>You could also go farther and read external configuration files at the root scope. The main thing to remember is that all intermediate variables will be visible outside, and thus should be prefixed with an underscore (Python's standard for private).</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
I thought of creating a class called Params and having a bunch of<br>
methods (decorated with @classmethod) that set/calculate the value of<br>
all the parameters. Easy enough, but then I have to create a Params<br>
object in every source file that uses these parameters, and that seems<br>
wasteful.<br></blockquote><div><br></div><div>If all the methods of a class as decorated with @classmethod or @staticmethod, and all variables are declared on the class, and not inside of any non-class/static methods (include __init__), you would not need to create instances of the object.</div>
<div><br></div><div>For example, if you have a class like follows (untested):</div><div><br></div><div>class Params():</div><div> myVar = 0</div><div> @classmethod</div><div> def myMethod(cls):</div><div> cls.myVar += 1</div>
<div> return cls.myVar</div><div><br></div><div>you can access them without an instance, by using Params.myVar or Params.myMethod().</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
The actual scope of the problem is very small, so memory/cpu time is not<br>
an issue. I'm just looking for the most pythonic/elegant way of doing this.<br></blockquote><div><br></div><div>For most configuration, I would recommend using a combination of the above. Create the module and place some configuration variables inside the module directly. Use classes (which shouldn't be instantiated) for namespacing. If you start to get a LOT of parameters, you may also want to consider making the entire params a package (folder/directory) with multiple modules and possibly classes, however that is likely overkill.</div>
<div><br></div><div>Note that, in any case, I would recommend treating the parameters as constants, meaning do not change their values, except during the initialization of the parameters module. This is mostly to ensure thread-safety and prevent difficult to track bugs.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
What is the recommended way of passing a bunch of static (hard-coded and calculated) parameters to various parts of the code?<br>
<br>
Thank you for any advice,<br>
<br>
Catherine<span class=""><font color="#888888"><br>
-- <br>
<a href="https://mail.python.org/mailman/listinfo/python-list" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div></div>