What's the use of the "with" statement in Python? : <p>While programming in Python, sometimes I need to launch functions and object methods, something like this:</p>

<pre><code>obj1.launch_method1(); // object method
do_something();        // general function
obj1.launch_method2(); // object method
</code></pre>

<p>Using the <code>with</code> statement, this becomes:</p>

<pre><code>with obj1:
  launch_method1();
  do_something();
  launch_method2();
</code></pre>

<p>In my opinion, this causes confusion, because the next programmer might erroneously think that <code>do_something()</code> is an object method rather than a general function.</p>

<p>In top of this, most IDEs has intellisense, so when you type <code>obj1.</code> (mind the dot), a list of methods and properties appears, which makes it pretty easy to type things like <code>obj1.launch_method1()</code>, <code>obj1.launch_method2()</code>, ...</p>

<p>So, from a programmer's perspective, there seems not to be an advantage in the usage of the <code>with</code> statement.</p>

<p>However, it seems that the <code>with</code> statement launches <code>__enter__</code> and <code>__exit__</code> calls, which seems to create new contexts. What are those calls? What does this mean? Does the usage of the <code>with</code> statement make any difference? If yes, which one(s)?</p>
