title: How to use contextmanagers for instance variables
body: <p>How would I use a Contextmanager for instance variables? E.g.
  Let's assume I've got some <code>Connection</code> class, that must be closed on destruction. If I were to implement it as a ContextManager I could do.</p>
  
  <pre><code>with Connection() as c:
      c.write('FOO')
      c.ask('BAR?')
  </code></pre>
  
  <p>and it would get automatically closed on destruction. But what if I wanted to use it in a <code>__init__</code> of another class, e.g. like the following example?</p>
  
  <pre><code>class Device(object):
      def __init__(self):
          self.connection = Connection()  # Must be closed on destruction.
  </code></pre>
  
  <p>I dont want it to be closed on exit of the constructor, it should die when the object get's destroyed. I could use <code>__del__</code> but this has it's downsides. Being used to RAII in C++ it baffles me. </p>
  
  <p>So what is the best way to do it in this case?</p>