Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Rob Gaddi Newsgroups: comp.lang.python Subject: Re: initializing "parameters" class in Python only once? Date: Mon, 14 Jul 2014 16:07:04 -0700 Organization: Highland Technology, Inc. Lines: 44 Message-ID: <20140714160704.11ac7b4d@rg.highlandtechnology.com> References: <53C4589A.9040109@jpl.nasa.gov> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Info: mx05.eternal-september.org; posting-host="b53b291bf17f07a266157ba92eeb40f6"; logging-data="19173"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FCFA+MZ8ncf8qXc0BmcAq" X-Newsreader: Claws Mail 3.9.3 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Cancel-Lock: sha1:CpJHBekaz1//a3gbGQDu9umjaKk= Xref: csiph.com comp.lang.python:74443 On Mon, 14 Jul 2014 15:24:26 -0700 Catherine M Moroney 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. > > 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. > > 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. > > 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 You're 90% of the way there. You can just create params.py, fill it with statements that are either hard-coded or calculated as necessary, and import params from all over the rest of the program. Once the interpreter has imported it for the first time, everyone else just gets a link to the same instance of the module rather than running it all over and over again. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix.