I have a list of Spam objects:
class Spam:
def update(self):
print('updating spam!')
some of them might be SpamLite objects:
class SpamLite(Spam):
def update(self):
print('this spam is lite!')
Spam.update(self)
I would like to be able to take an arbitrary object from the list, and add something to it’s update method, something like:
def poison(spam):
tmp = spam.update
def newUpdate(self):
print 'this spam has been poisoned!'
tmp(self)
spam.update = newUpdate
I want spam.update() to now either print:
this spam has been poisoned!
updating spam!
or
this spam has been poisoned!
this spam is lite!
updating spam!
depending on whether it was a SpamLite or just a Spam.
But that doesn’t work, because spam.update() won’t pass in the self argument automatically, and because if tmp leaves scope or changes then it won’t call the old update. Is there a way I can do this?
Full Script:
Output: