Reputation: 23200
I want to add event to list such that on adding items actions are taken based on the item e.g. genrating new data structures, change in screen output or raising exception.
How do I accomplish this?
Upvotes: 2
Views: 1420
Reputation: 18431
You could create your own class that extends the list object:
class myList(list):
def myAppend(self, item):
if isinstance(item, list):
print 'Appending a list'
self.append(item)
elif isinstance(item, str):
print 'Appending a string item'
self.append(item)
else:
raise Exception
L = myList()
L.myAppend([1,2,3])
L.myAppend('one two three')
print L
#Output:
#Appending a list
#Appending a string item
#[[1, 2, 3], 'one two three']
Upvotes: 1