Replies: 3 comments 3 replies
-
IIRC you need to implement setting attributes for your module using |
Beta Was this translation helpful? Give feedback.
-
An alternative in pure micropython: wrap your c module in a .py module which simply imports from the c module and then you can monkey-patch away. ie. mymodule.py from ._mymodule import func # import from _mymodule.c Then your example code should work fine. |
Beta Was this translation helpful? Give feedback.
-
If I understand you correctly, what you want to achieve is something like this:
However, this will cause an "object has no attribute" error in MicroPython. One solution is to copy all module attributes to a dict, preferably one that supports dot notation. See "dotdict.py" for a simple implementation: _B='No such attribute: '
class Dot(dict):
def __getattr__(B,N):
if N in B:return B[N]
else:raise AttributeError(_B+N)
def __setattr__(B,N,V):
B[N]=V
def __delattr__(B,N):
if N in B:del B[N]
else:raise AttributeError(_B+N) With this in place, we can then do the same in MicroPython:
The "dotdict.py" will probably work on all ports, except a port that does not support |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey,
I have written a c module like described in the documentation.
Is it possible to allow overwriting a function in it (e.g. monkey patching) afterwards?
e.g.
Running this with the c module gets me
AttributeError: 'module' object has no attribute func
.I found the
MICROPY_CAN_OVERRIDE_BUILTINS
option. But I dont quite get how to use it / if it even applies to my usecase.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions