replace a method in class: how?

Brian Blais bblais at bryant.edu
Mon Jun 26 19:14:51 EDT 2006


Hello,

I want to replace a method in a class during run-time with another function.  I tried 
the obvious, but it didn't work:

class This(object):
     def update(self,val):
         print val

def another_update(obj,val):
     print "another",val

t=This()
t.update(5)
t.update=another_update
t.update(5)  # this one doesn't work, gives
# TypeError: another_update() takes exactly 2 arguments (1 given)


clearly it isn't seeing it as a method, just an attribute which happens to be a 
function.  Is there a preferred way to do this?


		thanks,

				Brian Blais


-- 
-----------------

              bblais at bryant.edu
              http://web.bryant.edu/~bblais



More information about the Python-list mailing list