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 lists. 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

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -