python - Is it possible to delete an instance of a class that automatically removes it from all lists in which it is an element? -
i have instance of class
appears element in multiple list
s. want delete instance , simultaneously remove every list
in element. possible?
one answer allow objects putting lists manage list membership. example, rather saying
lista.append(objecta)
you use
objecta.addtolist(lista)
this allow internally store list of list contain objecta
. then, when want delete objecta
, need call method this:
def cleanup(self): listtoclean in mylists: listtoclean.remove(self)
this put strong limitations on program though - example, if copy made of 1 of these lists, objects not have reference copy. have work assumption copy of list (don't forget slices copies, too) may have obsolete objects in it, mean want working original lists possible.
Comments
Post a Comment