Hook into adding / removing items from list in python -


i'd create drop-in replacement python's list, allow me know when item added or removed. subclass of list, or implements list interface equally well. i'd prefer solution don't have reimplement of list's functionality though. there easy + pythonic way it?

pseudocode:

class mylist(list):     # ...     def on_add(self, items):         print "added:"         in items:             print      # , same on_remove  l = mylist() l += [1,2,3] # etc. 

to see functions defined in list, can do

>>> dir(list) 

then see can override, try instance this:

class mylist(list):     def __iadd__(self, *arg, **kwargs):         print "adding"         return list.__iadd__(self, *arg, **kwargs) 

you need few more of them of it.


Comments