class Scope:
def __init__(self):
self._entered = None
def __enter__(self):
if self._entered is not None:
raise RuntimeError('%s was reentered' % self)
self._entered = True
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self._entered = False
del self.__dict__
def __setattr__(self, key, value):
if not self._entered:
raise RuntimeError('%s is not active' % self)
super().__setattr__(key, value)