summaryrefslogtreecommitdiffstatshomepage
path: root/py/objcomplex.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-08-12 19:57:52 +0100
committerDamien George <damien.p.george@gmail.com>2014-08-12 19:57:52 +0100
commitf20375eedd22eeee5f1b273f891631bed8878cdc (patch)
tree7c2dec0af9cf191746cf7ea742e596c5fe531e86 /py/objcomplex.c
parentbb91f1195aec8eaca6d8f528118496cff71a101b (diff)
downloadmicropython-f20375eedd22eeee5f1b273f891631bed8878cdc.tar.gz
micropython-f20375eedd22eeee5f1b273f891631bed8878cdc.zip
py: Add .real and .imag attributes to complex numbers.
Diffstat (limited to 'py/objcomplex.c')
-rw-r--r--py/objcomplex.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/py/objcomplex.c b/py/objcomplex.c
index 20e7c97d37..0c58dee659 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -132,6 +132,15 @@ STATIC mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
}
+STATIC void complex_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
+ mp_obj_complex_t *self = self_in;
+ if (attr == MP_QSTR_real) {
+ dest[0] = mp_obj_new_float(self->real);
+ } else if (attr == MP_QSTR_imag) {
+ dest[0] = mp_obj_new_float(self->imag);
+ }
+}
+
const mp_obj_type_t mp_type_complex = {
{ &mp_type_type },
.name = MP_QSTR_complex,
@@ -139,6 +148,7 @@ const mp_obj_type_t mp_type_complex = {
.make_new = complex_make_new,
.unary_op = complex_unary_op,
.binary_op = complex_binary_op,
+ .load_attr = complex_load_attr,
};
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {