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: 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 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> In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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~