self-aware list of objects able to sense constituent member alterations?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Jan 29 06:41:40 CET 2009


On Wed, 28 Jan 2009 11:06:21 -0800, Reckoner wrote:

> thanks for your reply.
> 
> For the second case where
> 
>> class TaintThing(object):
>>     parent = A
>>     def __init__(self, obj):
>>         self.__dict__['_proxy'] = obj
>>     def __getattr__(self, attr):
>>         return getattr(self._proxy, attr)
>>     def __setattr__(self, attr, value):
>>         setattr(self._proxy, attr, value)
>>         self.parent.taint()
> 
> you have told it that parent is 'A'. Shouldn't that be passed to it
> somehow in the following:
> 
>> # untested
>> class TaintList(list):
>>     def append(self, obj):
>>         list.append(self, TaintThing(obj))
>>     # similar for __setitem__, extend, insert


Sure. Just change the initialisation of TaintThing to something like this:

    #untested
    def __init__(self, obj, parent):
        self.__dict__['_proxy'] = obj
        self.parent = parent


and then TaintList to something like this:

    def append(self, obj):
        list.append(self, TaintThing(obj, self))
        # similar for __setitem__, extend, insert





-- 
Steven



More information about the Python-list mailing list