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


Groups > comp.lang.python > #28168

Re: Context manager to save/restore a name binding

Date 2012-08-31 12:06 +0100
From Chris Withers <chris@python.org>
Subject Re: Context manager to save/restore a name binding
References <87oblrbxea.fsf@benfinney.id.au>
Newsgroups comp.lang.python
Message-ID <mailman.4006.1346411157.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Context manager to save/restore a name binding Ben Finney <ben+python@benfinney.id.au> - 2012-08-31 12:36 +1000
  Re: Context manager to save/restore a name binding Peter Otten <__peter__@web.de> - 2012-08-31 08:27 +0200
    Re: Context manager to save/restore a name binding Ben Finney <ben+python@benfinney.id.au> - 2012-08-31 19:32 +1000
  Re: Context manager to save/restore a name binding Chris Withers <chris@python.org> - 2012-08-31 12:06 +0100

csiph-web