replace mothod for only one object but not for a class

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Oct 15 10:45:35 CEST 2008


Bruno Desthuilliers a écrit :
> hofer a écrit :
>> Hi,
>>
>> I have multiple objects all belonging to the same class
>>  (which I didn't implement and whose code I don't want to modify)
>>
>> Now I'd like to change one method for one object only (after it has
>> been created) without adding any overhead
>> to the call of the other object's methods.
>>
>>
>> Is this possible?
> 
> Yes.
> 
> If the class is a new-style one [1], it just requires invoking the 
> descriptor protocol by yourself to get a bound method, ie:
> 
(snip)


> If it's a classic class, you can get by using either new.instancemethod 
> or types.MethodType:

Either I'm starting to get old or I need some vacations... Manually 
invoking function.__get__(obj, type(obj)) also work with classic 
classes, of course.

(snip)



More information about the Python-list mailing list