initializing "parameters" class in Python only once?

Chris Angelico rosuav at
Tue Jul 15 01:41:48 CEST 2014

On Tue, Jul 15, 2014 at 8:32 AM, Catherine M Moroney
<Catherine.M.Moroney at> wrote:
> 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.

Small job? Use the simplest possible technique. Just create
"" with a bunch of assignments in it:

a = 1
b = 2
c = a + b

# every other file
import params
print("c is",params.c)
# if you need to change anything:
params.c += 5
# everyone else will see the change, because there can be
# only one instance of the module (Highlander!)

Works nicely for anything even moderately complex. Also serves as a
convenient way to separate configs from code; for instance, I do this
any time I need to have a program with database passwords, or
per-installation setup, or stuff like that. Two examples:

In the latter case, doesn't even exist in the repository, as
its main purpose is to store the database connection string - both
private (don't want that published on Github) and per-installation (my
dev and production systems use different connection strings). The
Yosemite config is actually a bit legacy now; I used to have two
distinctly different instances of it, one running on Windows and the
other on Linux, but now I have a large number of identical instances
(all on Linux and all referencing the same disk server - which,
incidentally, is the one that I've weaponized with Alice, Elsa, Anya,
a Vorpal blade, and a Portal turret). Either way, though,
consists generally of simple assignments (and comments), but it's most
welcome to use all the power of Python to calculate values.


More information about the Python-list mailing list