"Inline methods, skip validations" traceMe = False def trace(*args): if traceMe: print('[' + ' '.join(map(str, args)) + ']') def accessControl(failIf): def onDecorator(aClass): class onInstance: def __init__(self, *args, **kargs): self.__wrapped = aClass(*args, **kargs) # Intercept and delegate built-in implicit access specifically def __add__(self, other): return self.__wrapped + other # Or getattr(), __getattr__() def __str__(self): return str(self.__wrapped) # Or self.__wrapped.__str__() def __getitem__(self, index): return self.__wrapped[index] def __call__(self, *args, **kargs): return self.__wrapped(*args, **kargs) # Plus any others needed # Intercept and delegate explicit attribute access generically def __getattr__(self, attr): trace('get:', attr) if failIf(attr): raise TypeError('private attribute fetch, ' + attr) else: return getattr(self.__wrapped, attr) def __setattr__(self, attr, value): trace('set:', attr, value) if attr == '_onInstance__wrapped': self.__dict__[attr] = value elif failIf(attr): raise TypeError('private attribute change, ' + attr) else: setattr(self.__wrapped, attr, value) return onInstance return onDecorator def Private(*attributes): return accessControl(failIf=(lambda attr: attr in attributes)) def Public(*attributes): return accessControl(failIf=(lambda attr: attr not in attributes))