Adding bound methods dynamically... CORRECTED

bruno modulix onurb at xiludom.gro
Thu Sep 1 19:34:32 CEST 2005

Mike Meyer wrote:
> bruno modulix <onurb at xiludom.gro> writes:
>>Devan L wrote:
>>>Kevin Little wrote:
>>>>I want to dynamically add or replace bound methods in a class.  
>>>I'm not an expert, but why do you need to dynamically add or replace
>>>bound methods?
>>To modify the behaviour at runtime ?-)
> Yes, but rather than going through the contortions you do to bind a
> new method into place, 

Contortion ? Which contortion ?

class Foo(object):
    def __init__(self, name): = name

def greet1(self, who):
    print "hello %s, this is %s" % (who,

def greet2(self, who):
    print "Yo %s, have a drink with Daddy %s" % (who,

f = Foo('Python')

Foo.greet = greet1
Foo.greet = greet2

> why not make the method in question act as a
> proxy for the real method? 

Please show us an implementation.

bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"

More information about the Python-list mailing list