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


Groups > comp.lang.python > #74620

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
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; 'subject:Python': 0.06; 'assignment': 0.07; 'sys': 0.07; '===': 0.09; 'debugging.': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'parameter': 0.09; 'params': 0.09; '~ethan~': 0.09; 'def': 0.12; '*end*': 0.16; '@property': 0.16; 'deletion': 0.16; 'subject:class': 0.16; 'subject:parameters': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'replace': 0.24; 'header:In-Reply-To:1': 0.27; "skip:' 10": 0.31; "d'aprano": 0.31; 'object.': 0.31; 'steven': 0.31; 'class': 0.32; "i'd": 0.34; 'definition': 0.35; 'but': 0.35; 'really': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'itself': 0.39; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:173': 0.61; 'received:69.56': 0.68; 'add:': 0.84
Date Wed, 16 Jul 2014 21:43:48 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: initializing "parameters" class in Python only once?
References <53C4589A.9040109@jpl.nasa.gov> <53c4bbf2$0$2746$c3e8da3$76491128@news.astraweb.com> <lq7g9u$rmh$1@dont-email.me>
In-Reply-To <lq7g9u$rmh$1@dont-email.me>
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator3304.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source-IP 173.12.184.233
X-Exim-ID 1X7dXo-0008Bi-4I
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender ([173.12.184.233]) [173.12.184.233]:45541
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20=
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.11915.1405576296.18130.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405576296 news.xs4all.nl 2919 [2001:888:2000:d::a6]:33865
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74620

Show key headers only | View raw


On 07/16/2014 08:35 PM, alex23 wrote:
> On 15/07/2014 3:28 PM, Steven D'Aprano wrote:
>> # === module params.py ===
>> class Params(object):
>>      a = 1
>>      b = 2
>>
>>      @property
>>      def c(self):
>>          return self.a**2 + self.b**2 - self.a + 1
>>
>> params = Params()
>> del Params  # hide the class
>>
>>
>> Then callers just say:
>>
>> from params import params
>> print params.c
>
> I'd replace the instantiation & deletion of the class in params.py with:
>
>      import sys
>      sys.modules[__name__] = Params()
>
> ..and replace the module itself with the parameter object. I'd also add:
>
>      __file__ = __file__
>
> ...to the class definition to help with debugging. But this is really just bikeshedding.

Just make sure the 'sys.modules' assignment happens at the *end* of params.py.

--
~Ethan~

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:24 -0700
  Re: initializing "parameters" class in Python only once? Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-07-14 16:07 -0700
  Re: initializing "parameters" class in Python only once? Chris Kaynor <ckaynor@zindagigames.com> - 2014-07-14 16:21 -0700
  Re: initializing "parameters" class in Python only once? Ben Finney <ben@benfinney.id.au> - 2014-07-15 10:04 +1000
  Re: initializing "parameters" class in Python only once? Steven D'Aprano <steve@pearwood.info> - 2014-07-15 05:28 +0000
    Re: initializing "parameters" class in Python only once? alex23 <wuwei23@gmail.com> - 2014-07-17 13:35 +1000
      Re: initializing "parameters" class in Python only once? Steven D'Aprano <steve@pearwood.info> - 2014-07-17 04:27 +0000
      Re: initializing "parameters" class in Python only once? Ethan Furman <ethan@stoneleaf.us> - 2014-07-16 21:43 -0700

csiph-web