Is this possible in Python ?

Carel Fellinger carel.fellinger at chello.nl
Thu May 15 14:42:47 EDT 2003


On Thu, May 15, 2003 at 05:38:37PM +0200, Carel Fellinger wrote:
...
Shipping code without testing again he? bad bad, the code that is:)

It works for one instance, but ruins it for all other instances, so we
need to class-jugle anyhow.  Here are the corrected monitor functions:

    def monitor_on(self, name, getter, setter, deller, doc):
        current = getattr(self, name)
        delattr(self, name)
        if not hasattr(self, "_Monitor__monitoring"):
            class Monitor(self.__class__):
                __monitoring = {}
            self.__class__ = Monitor
        self._Monitor__monitoring[name] = True
        setattr(self.__class__, name, property(getter, setter, deller, doc))
        setattr(self, name, current)


    def monitor_off(self, name):
        current = getattr(self, name)
        del self._Monitor__monitoring[name]
        if not self._Monitor__monitoring:
            self.__class__ = self.__class__.__bases__[0]
        else:
            delattr(self.__class__, name)
        setattr(self, name, current)


-- 
groetjes, carel





More information about the Python-list mailing list