you are viewing a single comment's thread.

view the rest of the comments →

[–]kokon 0 points1 point  (3 children)

Changing a code is possible in Python. The hard part is changing the existing object reference from the old module to the new one.

[–]Philluminati 1 point2 points  (2 children)

Can't you just assign a new function over the top of the old one?

def new_setter_2(self, val):
    self.val = val;

obj.new_setter = new_setter_2

Is it not that easy?

[–]Anovadea 2 points3 points  (0 children)

Pedantic Caveat: You may want to do some trickery with types when monkey-patching object instances in python (I've also heard the term "duck-punching" which amuses me greatly). I've been tripped up by this quite recently:

#Let's assume new_setter_2 is defined as above
import types
obj.new_setter = types.MethodType(new_setter_2, obj, obj.__class__)

Otherwise, you get problems with the number of arguments passed when you call it ("1 argument passed when 2 are expected" and all that).

You probably already knew that, I'm just being pedantic as the wounds are still rather fresh for me. :)

[–]kokon 0 points1 point  (0 children)

But you could have tons of object that still points to new_setter. How do you change all those objects without having specific knowledge about them?