Skip to content
Advertisement

unbind method from instance and bind to other instance

I have read this. is it possible to change: a1.show to a2.show, I mean, change the orientation of the method to point to different instance.

JavaScript

I want to see 2 in console. I mean, for a normal method in class, change its instance, from a1 to a2?


You may wonder why I do this, I have got a decorator to record what the instance has been through.

JavaScript

Now, I can add this decorator to the function that needs to be recorded every call. For example, I want linear to be recorded but wrap.

JavaScript

Now, I am allowed to define a simulate function, which pass in another instance, and let it be through what this instance has been through.

But, my cache only recorded foo.__name__, which I need to write my own mapper to decide which function to call. This is complex. Therefore, instead of recording foo.__name__, I want to directly record foo and change its orientation from self to other.

Hope I have explained with enough clarity. I will be so glad if you give me a hand.

Advertisement

Answer

I just noticed python object’s methods are not bonded to instance, which if I store foo in record, I need to pass in self as first argument.

JavaScript

This works out.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement