Re: Context manager to save/restore a name binding

2012-08-31 Thread Chris Withers
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 con

Re: Context manager to save/restore a name binding

2012-08-31 Thread Ben Finney
Peter Otten <__pete...@web.de> writes: > You should wrap yield in a try ... finally. You might allow setting > the new value in the manager (untested): Thank you, both good advice. I would still like to know if Python already has something to make this unnecessary. -- \ “Compulsory unifi

Re: Context manager to save/restore a name binding

2012-08-30 Thread Peter Otten
Ben Finney wrote: > I have written a context manager to save and restore a name binding:: > > import contextlib > > @contextlib.contextmanager > def preserve_value(namespace, name): > """ A context manager to preserve, then restore, the specified > binding. > >