py: Implement __delitem__ method for classes.
This commit is contained in:
parent
bc9ec5002d
commit
75ffcaeace
14
py/objtype.c
14
py/objtype.c
@ -309,16 +309,20 @@ STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool class_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
bool class_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||||
|
mp_obj_class_t *self = self_in;
|
||||||
|
mp_obj_t member;
|
||||||
|
uint meth_args;
|
||||||
if (value == MP_OBJ_NULL) {
|
if (value == MP_OBJ_NULL) {
|
||||||
// delete item
|
// delete item
|
||||||
// TODO implement me!
|
member = mp_obj_class_lookup(self->base.type, MP_QSTR___delitem__);
|
||||||
return false;
|
meth_args = 2;
|
||||||
|
} else {
|
||||||
|
member = mp_obj_class_lookup(self->base.type, MP_QSTR___setitem__);
|
||||||
|
meth_args = 3;
|
||||||
}
|
}
|
||||||
mp_obj_class_t *self = self_in;
|
|
||||||
mp_obj_t member = mp_obj_class_lookup(self->base.type, MP_QSTR___setitem__);
|
|
||||||
if (member != MP_OBJ_NULL) {
|
if (member != MP_OBJ_NULL) {
|
||||||
mp_obj_t args[3] = {self_in, index, value};
|
mp_obj_t args[3] = {self_in, index, value};
|
||||||
mp_call_function_n_kw(member, 3, 0, args);
|
mp_call_function_n_kw(member, meth_args, 0, args);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# test class with __getitem__ and __setitem__ methods
|
# test class with __getitem__, __setitem__, __delitem__ methods
|
||||||
|
|
||||||
class C:
|
class C:
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
@ -8,6 +8,10 @@ class C:
|
|||||||
def __setitem__(self, item, value):
|
def __setitem__(self, item, value):
|
||||||
print('set', item, value)
|
print('set', item, value)
|
||||||
|
|
||||||
|
def __delitem__(self, item):
|
||||||
|
print('del', item)
|
||||||
|
|
||||||
c = C()
|
c = C()
|
||||||
print(c[1])
|
print(c[1])
|
||||||
c[1] = 2
|
c[1] = 2
|
||||||
|
del c[3]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user