I have a list of Spam objects:
class Spam: def update(self): print('updating spam!')
some of them may 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 update method, for example:
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 SpamLite or just spam.
But this does not work, because spam.update () will not be automatically passed in the self argument, but because if tmp leaves visibility or changes, then it is not called by the old update. Is there a way I can do this?
source share