mirror of
https://github.com/micropython/micropython.git
synced 2026-01-07 04:30:24 +01:00
Implement __bool__ and __len__ via unary_op virtual method for all types.
__bool__() and __len__() are just the same as __neg__() or __invert__(), and require efficient dispatching implementation (not requiring search/lookup). type->unary_op() is just the right choice for this short of adding standalone virtual method(s) to already big mp_obj_type_t structure.
This commit is contained in:
@@ -73,7 +73,7 @@ static mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
|
||||
static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
|
||||
mp_obj_complex_t *o = o_in;
|
||||
switch (op) {
|
||||
case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; }
|
||||
case RT_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
|
||||
case RT_UNARY_OP_POSITIVE: return o_in;
|
||||
case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
|
||||
default: return MP_OBJ_NULL; // op not supported
|
||||
|
||||
Reference in New Issue
Block a user