Page 1 of 1

[SOLVED] static/class method in micropython?

Posted: Tue Sep 11, 2018 6:31 am
by shawwwn
Judging from the source, currently class method in micropython is not supported.
The underlying C struct of mp class instance only has .locals_dict.

Any plans to add support for class or static method?
This feature would be really useful for module developers.

Thanks.

Re: static/class method in micropython?

Posted: Tue Sep 11, 2018 6:51 am
by stijn
Actually class methods (assuming you mean methods inside a class decorated with @classmethod) are supported. Look at the mp_type_classmethod type.

Re: static/class method in micropython?

Posted: Tue Sep 11, 2018 7:44 am
by shawwwn
Nice. Thanks for pointing that out.

I am finding a way to implement static method in a python class written in C.
Any ideas how it can be done?

Re: static/class method in micropython?

Posted: Tue Sep 11, 2018 8:13 am
by shawwwn
Nvm. I found it.

Inside py/obj.h,
there is a macro called

Code: Select all

MP_DEFINE_CONST_STATICMETHOD_OBJ
which puts a staticmethod wrap onto a function.

Thanks for the help!
I'm closing this issue.