import functools class MyList (list): def append(self, v): list.append(self, v) class _ProxyType (type): pass class ProxyObject (object): def __init__(self, obj): self._obj = obj def __getattribute__(self, nm): if nm == '_obj' or nm in ProxyObject.__dict__: return object.__getattribute__(self, nm) try: v = object.__dict__[nm] except KeyError: raise AttributeError(nm) if isinstance(v, (type(lambda:1), type(list.append))): @functools.wraps(v) def method(self, *args, **kwds): return v(self._obj, *args, **kwds) #ProxyObject.__dict__[nm] = method setattr(ProxyObject, nm, method) return object.__getattribute__(self, nm) return v class ProxyList (ProxyObject): def __getattribute__(self, nm): if nm == '_obj' or nm in ProxyList.__dict__: return object.__getattribute__(self, nm) try: v = list.__dict__[nm] except KeyError: raise AttributeError(nm) if isinstance(v, (type(lambda:1), type(list.append))): @functools.wraps(v) def method(self, *args, **kwds): return v(self._obj, *args, **kwds) #ProxyList.__dict__[nm] = method setattr(ProxyList, nm, method) return object.__getattribute__(self, nm) return v def __getattribute_in_class__(self, tp, nm): print("getattribute_in_class", tp, nm) if tp is not ProxyList: raise AttributeError(nm) try: v = list.__dict__[nm] except KeyError: raise AttributeError(nm) if isinstance(v, (type(lambda:1), type(list.append))): @functools.wraps(v) def method(self, *args, **kwds): return v(self._obj, *args, **kwds) #ProxyList.__dict__[nm] = method setattr(ProxyList, nm, method) return object.__getattribute__(self, nm) return v class ProxyMyList (ProxyList): def __getattribute__(self, nm): if nm == '_obj' or nm in ProxyMyList.__dict__: return object.__getattribute__(self, nm) try: v = MyList.__dict__[nm] except KeyError: raise AttributeError(nm) if isinstance(v, (type(lambda:1), type(list.append))): @functools.wraps(v) def method(self, *args, **kwds): return v(self._obj, *args, **kwds) #ProxyMyList.__dict__[nm] = method setattr(ProxyMyList, nm, method) return object.__getattribute__(self, nm) return v v = ProxyMyList(MyList()) print("Before accessing") print("PL", ProxyList.__dict__) print("PML", ProxyMyList.__dict__) print("Accessing append", v.append) print("PL", ProxyList.__dict__) print("PML", ProxyMyList.__dict__) #print("Accessing list append", ProxyList(list()).append) #print("PL", ProxyList.__dict__) #print("PML", ProxyMyList.__dict__) print("Accessing super", super(ProxyMyList, v).append) print("PL", ProxyList.__dict__) print("PML", ProxyMyList.__dict__)