replace mothod for only one object but not for a class

hofer blabla at dungeon.de
Wed Oct 15 05:21:51 EDT 2008


On Oct 14, 7:50 pm, hofer <bla... at dungeon.de> wrote:
> 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?
>
> Example
> ##### This is NOT what I'd like to do
> ##### as it overwrites the method for all objects of this class
> o1 = aclass()
> o2 = aclass()
> # call original method
> o1.method()
> o2.method()
> # overwrite the method for the entire class
> aclass.method = mymethod
> o1.method() # now new method
> o2.method() # now new method
>
> ####### What doesn't work, but what I'd like to do
> o1 = aclass()
> o2 = aclass()
> # call original method
> o1.method()
> o2.method()
> # overwrite the method for the entire class
> o1.method = mymethod
> o1.method() # now new method
> o2.method() # still old method
>
> thanks for any pointers.
>
> P.S. I guess, that there is a computer science term for what I try to
> achieve.
> If anybody knew it I would be interested to learn it as well.


Thanks a lot this works.
Though I must admint, that even after reading the doc I don't really
understand why.



More information about the Python-list mailing list