def _allow_new_attributes(f):
 def decorated(self, *args, **kw):
  'The decorated function that replaces __init__() or __setstate__()\n\n    '
  if (not hasattr(self, '_canAddAttributes')):
   self.__dict__['_canAddAttributes'] = 1
  else:
   self._canAddAttributes += 1
  assert (self._canAddAttributes >= 1)
  count = self._canAddAttributes
  f(self, *args, **kw)
  if hasattr(self, '_canAddAttributes'):
   self._canAddAttributes -= 1
  else:
   self._canAddAttributes = (count - 1)
  assert (self._canAddAttributes >= 0)
  if (self._canAddAttributes == 0):
   del self._canAddAttributes
 decorated.__doc__ = f.__doc__
 decorated.__name__ = f.__name__
 return decorated

def _allow_new_attributes ( f ) : def decorated ( self , * args , ** kw ) : if ( not hasattr ( self , '_canAddAttributes' ) ) : self . __dict__ [ '_canAddAttributes' ] = 1 else : self . _canAddAttributes += 1 assert ( self . _canAddAttributes >= 1 ) count = self . _canAddAttributes f ( self , * args , ** kw ) if hasattr ( self , '_canAddAttributes' ) : self . _canAddAttributes -= 1 else : self . _canAddAttributes = ( count - 1 ) assert ( self . _canAddAttributes >= 0 ) if ( self . _canAddAttributes == 0 ) : del self . _canAddAttributes decorated . __doc__ = f . __doc__ decorated . __name__ = f . __name__ return decorated
a decorator that maintains the attribute lock state of an object it coperates with the lockattributesmetaclass that replaces the __setattr__ method with a custom one that checks the _canaddattributes counter and allows setting new attributes only if _canaddattributes > 0 .