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


Groups > comp.lang.python > #74607

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

From alex23 <wuwei23@gmail.com>
Newsgroups comp.lang.python
Subject Re: initializing "parameters" class in Python only once?
Date 2014-07-17 13:35 +1000
Organization A noiseless patient Spider
Message-ID <lq7g9u$rmh$1@dont-email.me> (permalink)
References <53C4589A.9040109@jpl.nasa.gov> <53c4bbf2$0$2746$c3e8da3$76491128@news.astraweb.com>

Show all headers | View raw


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.

It's a shame the property decorator doesn't work at the module level, 
though.

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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