Adding bound methods dynamically... CORRECTED

bruno modulix onurb at xiludom.gro
Thu Sep 1 13:34:32 EDT 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.  
>>
>>(snip)
>>
>>
>>>I'm not an expert, but why do you need to dynamically add or replace
>>>bound methods?
>>
>>To modify the behaviour at runtime ?-)
>>
(snip)
> 
> 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):
        self.name = name

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

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

f = Foo('Python')

Foo.greet = greet1
f.greet('Monty')
Foo.greet = greet2
f.greet('Monty')

> 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