title: Class variable in python context class
body: <p>I am writing a series of nested contexts, and need to keep trace of their relative relationship.  But I ran in the following behavior that I did not expect:</p>
  
  <pre><code>class NestedContext():
      _current = None
      def __enter__(self):
          self.parent = self._current
          self._current = self
  
      def __exit__(self, _type, _value, _tb):
           self._current = self.parent
  
  with NestedContext():
      assert NestedContext._current is not None # fails
      with NestedContext():
          pass
  </code></pre>
  
  <p>The assert fail because the class variable <code>_current</code> is None, which is unexpected, since I thought <code>__enter__</code> just set it to self.  This seems to be related some deeper behavior of Python context that I don't understand.  Can anyone explain?  Thanks.</p>