If you are on a similar learning path you might be able to use this work. The file is linked below.
https://github.com/MikeTeachman/MicroPy ... examples.c
This file can be compiled into a build and run by importing the "argex" module. I developed it on the ESP32/Lobo port, but I expect it will work for all uPy ports.
Each example shows:
- uPy interface to the function or method, showing all possible variants of positional and keyword arguments
- C implementation
Code: Select all
// Method Example 3:
// variable number of positional arguments, specified by min and max
// NOTE: for a method call, MIN_NUM_METHOD_ARGS must never be zero
//
// class Argex:
// m_min_max(self, [pos1]):
//
// from argex import Argex
// a = Argex(True, 7)
// a.m_min_max()
// a.m_min_max(False)
#define MIN_NUM_METHOD_ARGS (1) // self
#define MAX_NUM_METHOD_ARGS (2) // self + one Optional positional argument
STATIC mp_obj_t method_min_max_args(size_t n_args, const mp_obj_t *args) {
argex_obj_t *self = MP_OBJ_TO_PTR(args[0]);
// important notes:
// 'n_args' refers to the count of positional args, and +1 for the class instance self
printf("n_args = %u\n", n_args);
if (n_args == 1) {
// when n_args equals 1 it means that ONLY argument passed is the class instance self
return mp_obj_new_int(self->var1);
} else {
self->var1 = mp_obj_get_int(args[1]);
return mp_const_none;
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(method_min_max_args_obj, MIN_NUM_METHOD_ARGS, MAX_NUM_METHOD_ARGS, method_min_max_args);