Small problem when overloading member functions...

laotseu bdesth at nospam.free.fr
Thu Jan 9 05:51:22 CET 2003


laotseu wrote:
(snip)
> What you're doing here is 'replacing' an instance method with another 
> one (I guess in Python's parlance, we should say 'rebinding').
> 
> If what you're trying to do is just that (replacing an instance method), 
>  do it the simplest way (the one that is not commented in your code).
> 
> Now if you want to 'replace' a method for all existing instances of a 
> class,  I leave it up to some __class__ Guru !-)
> 
> Laotseu
> 

Ok, I got it a few minutes later :

class Truc:
     def test(self):
         print "old test"

a = Truc()

def test2(self):
     print "new test"

a.__class__.test = test2
a.test()
 >>>new test

Laotseu





More information about the Python-list mailing list