use Userdict for class __dict__
Gang Li
gang.li at compuware.com
Thu Dec 2 14:27:57 EST 1999
The proxy only works with instance. I want something working with class,
and the class subclass it will inherent the same properties.
"Gordon McMillan" <gmcm at hypernet.com> wrote in message
news:1268037651-41672093 at hypernet.com...
> Gang Li writes:
>
> > 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__
>
>
> - Gordon
>
>
More information about the Python-list
mailing list