471,325 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Instrospection question

I have the following code:

--------------------------
import new

class A:
def a(self):
print "Original"

def other(cad):
return cad + " modified"

def replace_method(method):
def b(self,*args,**kwargs):
result = method(*args,**kwargs)
return other(result)
return b
a = A()

setattr(a,"a",new.instancemethod(replace_method(a. a) ,a,A))

a.a()

#Result should be:
# Original modified

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

As you can see, what I'm trying to do is "replace" a method with another
one wich is the same method but with a function applied to it (in this
case, a string concatenation ( +" modified"))

Can anybody help me with this?


Dec 21 '07 #1
4 916
On Dec 21, 11:28 am, Matias Surdi <matiassu...@gmail.comwrote:
I have the following code:
[...]
As you can see, what I'm trying to do is "replace" a method with another
one wich is the same method but with a function applied to it (in this
case, a string concatenation ( +" modified"))

Can anybody help me with this?
Why not use inheritance?

class A(object):
def a(self):
print "Original"

class B(A):
def a(self):
super(B, self).a()
print 'modified'

a = B()
a.a()

I'm not saying the code below is a good idea, but it solves the
problem of post-hoc modification

a = A()
a.__class__ = B
a.a()

we could probably come up with a better solution if we knew more about
the problem you were trying to solve...

-- bjorn
Dec 21 '07 #2
Matias Surdi <ma*********@gmail.comwrote:
I have the following code:

--------------------------
import new

class A:
def a(self):
print "Original"

def other(cad):
return cad + " modified"

def replace_method(method):
def b(self,*args,**kwargs):
result = method(*args,**kwargs)
return other(result)
return b
a = A()

setattr(a,"a",new.instancemethod(replace_method(a. a) ,a,A))
a.__dict__['a'] = new.instancemethod(replace_method(a.a),a,A)
>
a.a()

#Result should be:
# Original modified
You are unlikely to get that unless you make 'a' return a result. I get:
>>a.a()
Original

Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
a.a()
File "<pyshell#5>", line 4, in b
return other(result)
File "<pyshell#3>", line 2, in other
return cad + " modified"
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Dec 21 '07 #3
Duncan Booth wrote:
Matias Surdi <ma*********@gmail.comwrote:
>setattr(a,"a",new.instancemethod(replace_method(a .a) ,a,A))

a.__dict__['a'] = new.instancemethod(replace_method(a.a),a,A)
Or even

a.a = new.instancemethod(...)

Peter
Dec 21 '07 #4
Matias Surdi wrote:
I have the following code:

--------------------------
import new

class A:
def a(self):
print "Original"

def other(cad):
return cad + " modified"

def replace_method(method):
def b(self,*args,**kwargs):
result = method(*args,**kwargs)
return other(result)
return b
a = A()

setattr(a,"a",new.instancemethod(replace_method(a. a) ,a,A))

a.a()

#Result should be:
# Original modified

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

As you can see, what I'm trying to do is "replace" a method with another
one wich is the same method but with a function applied to it (in this
case, a string concatenation ( +" modified"))

Can anybody help me with this?
Change A.a() to return a value:

class A:
def a(self):
return "Original"

# ...

print a.a()

and it should work. You can then start to simplify:

class A:
def a(self):
return "Original"

def other(cad):
return cad + " modified"

def replace_method(method):
def b(*args,**kwargs):
result = method(*args,**kwargs)
return other(result)
return b

a = A()

a.a = replace_method(a.a)

print a.a()

Peter
Dec 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ismael Herrera | last post: by
3 posts views Thread by Stevey | last post: by
10 posts views Thread by glenn | last post: by
53 posts views Thread by Jeff | last post: by
56 posts views Thread by spibou | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
3 posts views Thread by Zhang Weiwu | last post: by

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.