Making a property in c code #9606
Replies: 3 comments 2 replies
-
To keep the code size down, I would suggest using attributes instead of properties. Properties require creating two function objects, one for getting and one for setting plus a property object for each property while all attributes, including getters and setters, are implemented in a single function with no extra Python objects. |
Beta Was this translation helpful? Give feedback.
-
Like @dlech said, properties are much more code-size expensive which is why they aren't used. You can support this by implementing the property in your type's CircuitPython uses properties extensively and so they have support for properties defined in C modules, e.g. DigitialInOut.value. We are unlikely to add this. STATIC mp_obj_t digitalio_digitalinout_obj_get_value(mp_obj_t self_in) {
...
}
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_value_obj, digitalio_digitalinout_obj_get_value);
STATIC mp_obj_t digitalio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
...
}
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_value_obj, digitalio_digitalinout_obj_set_value);
MP_PROPERTY_GETSET(digitalio_digitalinout_value_obj,
(mp_obj_t)&digitalio_digitalinout_get_value_obj,
(mp_obj_t)&digitalio_digitalinout_set_value_obj); |
Beta Was this translation helpful? Give feedback.
-
OK that's understandable. How do you go about setting instance attributes in c code?? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am tinkering about with organizing a common API for a CAN bus interface and one of the things I would like to do is create a class for CAN message and that class could then be passed to the send function or be returned from the receive function. The class would house the various parts of the message like the data, and frame id. That kind of stuff. I want to the user to be able to reuse the class if they want but in order to do that they need to be able to set the attributes within the class to whatever it is they want. The trick is with different ports of the CAN interface there needs to be a way to inject port specific code into getting and setting the data. I wanted to keep the memory foot print down so my thought was to set the ports actual structure for receiving and sending as a back end for the class.
There are macros for class methods and static methods
MP_DEFINE_CONST_STATICMETHOD_OBJ
MP_DEFINE_CONST_CLASSMETHOD_OBJ
but I am not finding anything for a properly.
Beta Was this translation helpful? Give feedback.
All reactions