Entry
Proxy class (was: use UserDict for class __dict__)
Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 339, Gordon McMillan
"""
Packages: oop;basic_datatypes.dictionaries
"""
"""
> In order to monitor access of class attributes, I tried to
> replace class __dict__ with UserDict.
>
> class Foo: pass
>
> class MyUserDict(UserDict.UserDict):
> def __getitem__(self, name):
> ......
>
> Foo.__dict__ = MyUserDict(Foo.__dict__)
>
> But python bit me with:
> TypeError: __dict__ must be a dictionary object
>
> How can I avoid this kind error.
Wrap your object in a proxy:
"""
class Proxy:
def __init__(self, obj=None):
self.__obj__ = obj
def __getattr__(self,name):
print "getattr called for", name
return getattr(self.__obj__, name)
def __repr__(self):
return "Proxy for "+`self.__obj__`
__str__ = __repr__