title: Python TemporaryDirectory returns string when used in "with" statement
body: <p>Python TemporaryDirectory returns string when used in "with" statement</p>
  
  <h2>Short Version of Question</h2>
  
  <p>Why does <code>TemporaryDirectory</code> return a string when used in a <code>with</code> context?</p>
  
  <h2>Longer Version of Question</h2>
  
  <p>Here is an example of some Python code which creates a temporary directory <code>tempdir</code> and prints the corresponding object:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; import tempfile
  &gt;&gt;&gt; tempdir = tempfile.TemporaryDirectory(dir="/tmp")
  &gt;&gt;&gt; print(tempdir)
  &lt;TemporaryDirectory '/tmp/tmpf2yh8xu9'&gt;
  
  &gt;&gt;&gt; print(type(tempdir))
  &lt;class 'tempfile.TemporaryDirectory'&gt;
  </code></pre>
  
  <p>As expected, <code>tempdir</code> is an instance of <code>TemporaryDirectory</code>.</p>
  
  <p>And here is a similar example, where I use a <code>with</code> statement when calling <code>TemporaryDirectory</code>:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; import tempfile
  &gt;&gt;&gt; with tempfile.TemporaryDirectory(dir="/tmp") as tempdir: print(tempdir)
  /tmp/tmp7mlmzegs
  
  &gt;&gt;&gt; with tempfile.TemporaryDirectory(dir="/tmp") as tempdir: print(type(tempdir))
  &lt;class 'str'&gt;
  </code></pre>
  
  <p>In this case, <code>tempdir</code> is a string. When I look at the <code>__enter__</code> method of the <code>TemporaryDirectory</code> class, I see the following:</p>
  
  <pre class="lang-py prettyprint-override"><code>def __enter__(self):
      return self.name
  </code></pre>
  
  <p>Sure enough - looks like a string is returned instead of the object itself.</p>
  
  <p>What accounts for this discrepancy? Why does the <code>__enter__</code> method return the file name instead of the file object?</p>