A "scopeguard" for Python

Mike Kent mrmakent at gmail.com
Thu Mar 4 22:19:44 CET 2010


On Mar 3, 12:00 pm, Robert Kern <robert.k... at gmail.com> wrote:
> 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()
> >     try:
> >         os.chdir(somewhere)
> >         # Do other stuff
> >     finally:
> >         os.chdir(original_dir)
> >         # Do other cleanup
>
> A custom-written context manager looks nicer and can be more readable.
>
> from contextlib import contextmanager
> import os
>
> @contextmanager
> def pushd(path):
>      original_dir = os.getcwd()
>      os.chdir(path)
>      try:
>          yield
>      finally:
>          os.chdir(original_dir)
>
> with pushd(somewhere):
>      ...

Robert, I like the way you think.  That's a perfect name for that
context manager!  However, you can clear one thing up for me... isn't
the inner try/finally superfluous?  My understanding was that there
was an implicit try/finally already done which will insure that
everything after the yield statement was always executed.



More information about the Python-list mailing list