In the Classes section under Properties (around page 40) there is an example which adds a .attr = to the type definition of a class
(propertyclass_type in the example).
I have tried to extend this example to have both a property and a method but when I try to run it the method is not found - I get the error message: AttributeError: 'propertyclass' object has no attribute 'name'
Looking at the source code in runtime.c around line 1091 there is the code
Code: Select all
} else if (type->attr != NULL) {
// this type can do its own load, so call it
type->attr(obj, attr, dest);
} else if (type->locals_dict != NULL) {
// generic method lookup
// this is a lookup in the object (ie not class or type)
assert(type->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now
mp_map_t *locals_map = &type->locals_dict->map;
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
if (elem != NULL) {
mp_convert_member_lookup(obj, type, elem->value, dest);
}
}
Is there an example for what the ..._attr method should look like to define both properties and methods?
The example in the referenced document has the code:
Code: Select all
STATIC void propertyclass_attr(mp_obj_t self, qstr attribute, mp_obj_t *destination) {
if(attribute == MP_QSTR_x) {
destination[0] = propertyclass_x(self);
} else if(attribute == MP_QSTR_y) {
destination[0] = propertyclass_y(self);
}
}