I simply wanted to know if you could call a function whenever a list is changed (item added to it). If anything it would look like this:
def listchanged():
    print("The list has been changed!")
list = ["apples","bananas"]
listevent(list,listchanged)
And in the shell you would append an item to the list and it would print "The list has been changed!"
Thanks!
You can subclass list to print when changing the list.
Here's an example:
class EventList(list):
    def __setitem__(self, key, value):
        super(EventList, self).__setitem__(key, value)
        print("The list has been changed!")
    def __delitem__(self, value):
        super(EventList, self).__delitem__(value)
        print("The list has been changed!")
    def __add__(self, value):
        super(EventList, self).__add__(value)
        print("The list has been changed!")
    def __iadd__(self, value):
        super(EventList, self).__iadd__(value)
        print("The list has been changed!")
    def append(self, value):
        super(EventList, self).append(value)
        print("The list has been changed!")
    def remove(self, value):
        super(EventList, self).remove(value)
l = EventList(["apples","bananas"])
l.append('test')
Prints:
The list has been changed!
OR
Just compare lists:
old_list = ["apples","bananas"]
new_list = ["apples","bananas"]
newer_list = ["apples","bananas", "oranges"]
old_list == new_list #true
old_list == newer_list #false
This would work with no subclass as == compares if the lists have the same elements in the same indicies not if they are exactly the same by id or hash (in lamens). Just save the old one in a variable, then when you want to check just use this. (note: this does not provide a way for the print to be automatically called. It's just another way)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With