replace a method in class: how?

B

Brian Blais

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
 
J

John Machin

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?

You have a strange definition of "obvious". You say you want to replace
a method in a *class*, not in an instance of that class ... so just do that:

|>> class This(object):
.... def update(self,val):
.... print val
....
|>> def another_update(obj,val):
.... print "another",val
....
|>> This.update = another_update
|>> t = This()
|>> t.update(42)
another 42

Cheers,
John
 
N

nate

I don't know a ton about this, but it seems to work if you use:
This.update = another_update
(instead of t.update = another_update)
after saying This.update = another_update, if you enter This.update, it
says <unbound method This.another_update>, (it's unbound)
but if you call
t.update(5)
it will print:
another 5
 
M

Maric Michaud

Le mardi 27 juin 2006 05:05, Bruno Desthuilliers a écrit :
import types
t.update = types.MethodType(another_update)

This works with :

t.update = types.MethodType(another_update, t)

Oh, this *really* misleading what the intent of the programmer is, and

In [29]: t.update
Out[29]: <bound method ?.<lambda> of <__main__.a instance at 0xa774d96c>>

all let think it's a method defined in t's class.
I prefer early binding with default parameters :

In [30]: def another_update(self, param) :pass
....:

In [31]: t.update = lambda x, self=t : another_update(self, x)

In [32]: t.update = lambda x, self=t, func=another_update : func(self, x)

In [33]: t.update
Out[33]: <function <lambda> at 0xa7744aac>

So we have a function and know it (probably) belongs to the instance.

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
B

Bruno Desthuilliers

Brian Blais a écrit :
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

This is not "replacing a method in a class", but replacing it in an
instance.
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?

import types
t.update = types.MethodType(another_update)
 
B

Bruno Desthuilliers

Maric Michaud a écrit :
Le mardi 27 juin 2006 05:05, Bruno Desthuilliers a écrit :



This works with :

t.update = types.MethodType(another_update, t)

oops ! too fast on the send button :(
And thanks for the correction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,129
Latest member
FastBurnketo
Top