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?

Date 2014-07-16 21:43 -0700
From Ethan Furman <ethan@stoneleaf.us>
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>
Newsgroups comp.lang.python
Message-ID <mailman.11915.1405576296.18130.python-list@python.org> (permalink)

Show all headers | 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