replace a method in class: how?

Maric Michaud maric at aristote.info
Tue Jun 27 02:06:48 CEST 2006


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



More information about the Python-list mailing list