title: How does wrapping an unsafe python method (e.g os.chdir) in a class make it thread/exception safe?
body: <p>In the question <a href="https://stackoverflow.com/questions/431684/how-do-i-cd-in-python">How do I "cd" in python</a>, the accepted answer recommended wrapping the os.chdir call in a class to make the return to your original dir exception safe. Here was the recommended code:</p>
  
  <pre><code>class Chdir:         
    def __init__( self, newPath ):  
      self.savedPath = os.getcwd()
      os.chdir(newPath)
  
    def __del__( self ):
      os.chdir( self.savedPath )
  </code></pre>
  
  <p>Could someone elaborate on how this works to make an unsafe call exception safe?</p>