initializing "parameters" class in Python only once?

Chris Kaynor ckaynor at zindagigames.com
Mon Jul 14 19:21:27 EDT 2014


On Mon, Jul 14, 2014 at 3:24 PM, Catherine M Moroney <
Catherine.M.Moroney at jpl.nasa.gov> wrote:

> 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.
>

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).

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).

For example, the following is a valid module (untested):
# Params.py
a = 1
b = 5
c = a * b + 42
d = c - b
# End Params.Py

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).

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.
>

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.

For example, if you have a class like follows (untested):

class Params():
    myVar = 0
    @classmethod
    def myMethod(cls):
        cls.myVar += 1
        return cls.myVar

you can access them without an instance, by using Params.myVar or
Params.myMethod().


> The actual scope of the problem is very small, so memory/cpu time is not
> an issue.  I'm just looking for the most pythonic/elegant way of doing
> this.
>

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.

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.


>
> What is the recommended way of passing a bunch of static (hard-coded and
> calculated) parameters to various parts of the code?
>
> Thank you for any advice,
>
> Catherine
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140714/6e66c38c/attachment.html>


More information about the Python-list mailing list