Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=python.org; s=200901; t=1346411156; bh=waF+5/zuptK5CRdgU2peSZ2M9ldtHG3zVAO0db4dzQI=; h=Message-ID:Date:From:MIME-Version:To:CC:Subject:References: In-Reply-To:Content-Type:Content-Transfer-Encoding; b=SEmp+6P8Qbw7iaDp5HdeZI7x5Hp5TGkN55Nr+cn1vaeJuiEN8kvOFqdAJ5Hhe+gqW yankiuoUNPbfOMykmNOgC2yC5j46tov4+lmWAoiUepn5YRDJgsF5LdpReLJ31BidHV skmjZJdsIGkI5xDNGLaIFwPYU9FqJy4xbEjja2Ls= X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.05; 'plenty': 0.07; 'testing,': 0.07; 'python': 0.09; '8bit%:30': 0.09; 'doing?': 0.09; 'from:name:chris withers': 0.09; 'received:89.151': 0.09; 'received:89.151.125': 0.09; 'received:89.151.125.140': 0.09; 'received:server1.simplistix.co.uk': 0.09; 'received:simplistix.co.uk': 0.09; 'restored': 0.09; 'cc:addr :python-list': 0.10; 'ben,': 0.16; 'exists?': 0.16; 'finney': 0.16; 'message-id:@python.org': 0.16; 'received:buzzkill.local': 0.16; 'simplistix': 0.16; 'url:packages': 0.16; 'url:simplistix': 0.16; 'from:addr:python.org': 0.17; 'wrote:': 0.17; 'code,': 0.18; 'from:addr:chris': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'subject:/': 0.28; 'chris': 0.28; 'at:': 0.31; 'code': 0.31; 'url:python': 0.32; 'whatever': 0.35; 'ben': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'depends': 0.36; 'previous': 0.37; 'subject:: ': 0.38; 'url:co': 0.66; '8bit%:21': 0.69; 'touched': 0.84; 'received:89': 0.86; 'received:86': 0.93 Date: Fri, 31 Aug 2012 12:06:04 +0100 From: Chris Withers User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: Ben Finney Subject: Re: Context manager to save/restore a name binding References: <87oblrbxea.fsf@benfinney.id.au> In-Reply-To: <87oblrbxea.fsf@benfinney.id.au> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346411157 news.xs4all.nl 6910 [2001:888:2000:d::a6]:37508 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28168 Hi Ben, On 31/08/2012 03:36, Ben Finney wrote: > That way, I can set ‘sys.dont_write_bytecode’ to the value I need in > this part of the code, knowing that however the code continues the > previous value of that setting will be restored to whatever it was > before I touched it. > > Have I re-invented a context manager which already exists? Is there a > better way to do what ‘preserve_value’ is doing? Depends on the context (ho ho..), but if it's testing, then have a look at: http://packages.python.org/testfixtures/mocking.html#the-context-manager There's plenty of other goodies you may like in there too... cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk