how to detect change of list of instances

manstey manstey at csu.edu.au
Wed Mar 14 02:23:24 CET 2007


how do I detect a change in a list of class instances?

from copy import deepcopy

class CaListOfObj(list):
    """ subclass of list """
    def __init__(self, *args, **kwargs):
        list.__init__(self, *args, **kwargs)

class CaClass(object):
    pass

class CaData(object):
    pass

myclass=CaClass()
a=CaData()
b=CaData()
c=CaData()

listInstances = CaListOfObj([a,b,c])
setattr(myclass,'initlist',listInstances)
setattr(myclass,'newlist',deepcopy(listInstances))

print myclass.initlist == myclass.newlist
myclass.newlist.append(c)
print myclass.initlist == myclass.newlist

gives
False
False

because deep copies of instances are different instances. what I want
to do is detect a change between .initlist and .newlist.

thanks




More information about the Python-list mailing list