A "scopeguard" for Python
Alf P. Steinbach
alfps at start.no
Wed Mar 3 18:09:53 CET 2010
* Robert Kern:
> On 2010-03-03 09:39 AM, Mike Kent wrote:
>> What's the compelling use case for this vs. a simple try/finally?
>> original_dir = os.getcwd()
>> # Do other stuff
>> # Do other cleanup
> A custom-written context manager looks nicer and can be more readable.
> from contextlib import contextmanager
> import os
> def pushd(path):
> original_dir = os.getcwd()
> with pushd(somewhere):
> I don't think a general purpose ScopeGuard context manager has any such
> benefits over the try: finally:, though.
I don't think that's a matter of opinion, since one is correct while the other
More information about the Python-list