Monkey Patching without types.MethodType

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Monkey Patching without types.MethodType

Post by cefn » Mon Jul 24, 2017 10:31 am

Monkey patching objects and classes is an option in Python3 but I'm struggling to figure out how to do it in Micropython in the absence of types.MethodType

https://filippo.io/instance-monkey-patching-in-python/

I was surprised to find with python that simply assigning a function to the Class or Object symbol table wasn't enough to bind a method. Coming from Javascript and Java and having had to put up with declaring self without having a this automatically bound, I had hoped that at least monkey-patching would be trivial, but something seems to be missing which means a function assigned as an attribute is not bound as a method.

It would help very much in development to be able to copy-paste method implementations to override the one in the module while debugging a problem, before uploading a new module with the method fixed (proven by the monkey patching).

Is this impossible in Micropython?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Monkey Patching without types.MethodType

Post by deshipu » Mon Jul 24, 2017 10:37 am

You have to make the function an attribute of the class, not of the object instance, for method binding to work. It's the same as in regular Python.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Monkey Patching without types.MethodType

Post by cefn » Tue Aug 01, 2017 5:19 pm

Thanks, Deshipu. Just getting back to this after a ton of other commitments. Very useful and could have wasted a bunch of time failing to realise I was approaching it wrong.

Post Reply