Instrospection question

M

Matias Surdi

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

thebjorn

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
 
D

Duncan Booth

Matias Surdi said:
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:
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'
 
P

Peter Otten

Matias said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top