Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74460

Re:initializing "parameters" class in Python only once?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'static': 0.04; 'classes,': 0.05; 'subject:Python': 0.06; 'duplicate': 0.07; 'problem:': 0.07; 'processing.': 0.07; 'assuming': 0.09; 'attributes': 0.09; 'debugging.': 0.09; 'latter': 0.09; 'methods,': 0.09; 'params': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'terms,': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'modules,': 0.16; 'once.': 0.16; 'parameters,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'simplest': 0.16; 'subject:class': 0.16; 'subject:parameters': 0.16; 'sure.': 0.16; 'thread,': 0.16; 'language': 0.16; 'module': 0.19; 'later': 0.20; 'seems': 0.21; 'import': 0.22; 'separate': 0.22; 'creating': 0.23; 'source': 0.25; 'class.': 0.26; 'certain': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'rest': 0.29; 'skip:@ 10': 0.30; "i'm": 0.30; 'code': 0.31; '3.x': 0.31; 'bunch': 0.31; 'calculated': 0.31; 'object.': 0.31; 'overhead': 0.31; 'parameters.': 0.31; 'question:': 0.31; 'file': 0.32; 'class': 0.32; 'there.': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'created': 0.35; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'there': 0.35; 'instances': 0.36; 'object,': 0.36; 'method': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'minimum': 0.38; 'to:addr:python- list': 0.38; 'rather': 0.38; 'explain': 0.39; 'received:71': 0.39; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'easy': 0.60; 'hope': 0.61; 'simply': 0.61; "you're": 0.61; 'refer': 0.63; 'different': 0.65; 'sole': 0.78; 'catherine': 0.84; 'how.': 0.84; 'pardon': 0.84; 'subject::': 0.85
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re:initializing "parameters" class in Python only once?
Date Mon, 14 Jul 2014 22:55:11 -0500 (CDT)
Organization news.gmane.org
References <53C45A6D.9040401@jpl.nasa.gov>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-71-97-195-105.hstntx.dsl-w.verizon.net
X-Newsreader PiaoHong.Usenet.Client.VIP:1.56
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11823.1405396545.18130.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405396545 news.xs4all.nl 2940 [2001:888:2000:d::a6]:39174
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74460

Show key headers only | View raw


Catherine M Moroney <Catherine.M.Moroney@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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

initializing "parameters" class in Python only once? Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> - 2014-07-14 15:32 -0700
  Re: initializing "parameters" class in Python only once? Chris Angelico <rosuav@gmail.com> - 2014-07-15 09:41 +1000
  Re: initializing "parameters" class in Python only once? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-15 01:05 +0100
  Re:initializing "parameters" class in Python only once? Dave Angel <davea@davea.name> - 2014-07-14 22:55 -0500

csiph-web