473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace a method in class: how?

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
--
-----------------

bb****@bryant.edu
http://web.bryant.edu/~bblais
Jun 26 '06 #1
5 6842
On 27/06/2006 9:14 AM, Brian Blais wrote:
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
Jun 26 '06 #2
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)
Jun 26 '06 #3
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

Jun 26 '06 #4
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
Jun 27 '06 #5
Maric Michaud a écrit :
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)


oops ! too fast on the send button :(
And thanks for the correction.
Jun 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8540
by: JHB | last post by:
Hi, How can I do a location.replace when I use a form, like when I use a href? This works. <a href="Ny HTML-side20.htm"; method="post" id="frm" name="BrugerHovedSide"...
5
11337
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
3
2385
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
23
1912
by: digitalorganics | last post by:
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj =...
3
3485
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
10
1885
by: Steven W. Orr | last post by:
In the program below, I want this instance to end up calling repmeth whenever inst.m1 is called. As it is now, I get this error: Hello from init inst = <__main__.CC instance at 0x402105ec>...
10
18610
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
1
3375
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
10
1427
by: hofer | last post by:
Hi, I have multiple objects all belonging to the same class (which I didn't implement and whose code I don't want to modify) Now I'd like to change one method for one object only (after it has...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.