6.3. C module variable parameters
[Note]
Requires kernel version
>= v1.10.0
Variable arguments must be placed after positional arguments
Example.
// test.c
void test_vals(PikaObj* self, int a, PikaTuple* val){
printf("a: %d\n", a);
for(int i =0; i< tuple_getSize(val); i++){
Arg* arg_i = tuple_getArg(val, i);
printf("val[%d]: %d\n", i, arg_getInt(arg_i));
}
}
Output the result:
>>> test.vals(1, 2, 3, 4)
a: 1
val[0]: 2
val[1]: 3
>>>
6.4. C module keyword parameters
[Note]
Requires kernel version
>= v1.10.7
Keyword arguments must be placed after positional and variable arguments
Example.
# test.pyi
def keys(a:int, **keys):...
Output result:
>>> test.keys(1, b=2, c=3)
a: 1
keys['b']: 2
keys['c']: 3
>>>
6.5. C module constants
class cJSON:
cJSON_Invalid: int
cJSON_False: int
def __init__(self):...
...
void pika_cjson_cJSON___init__(PikaObj* self) {
obj_setInt(self, "cJSON_False", cJSON_False);
...
}
These constants can be used directly without creating an object, i.e. as class properties.
Note that PikaScript class properties are read-only, and all modifications to class properties are invalid.
6.6. C module initialization
Define __init__()
function directly in .pyi to perform module initialization, which will be triggered when the module is loaded, PikaScript has a delayed module loading mechanism, import
will not trigger module loading directly, but only when the module is actually used for the first time.
For example:
# test.pyi
def __init__():...
def hello():...
//test.c
void test___init__(PikaObj* self){
printf("now loading module test...\n");
}
void test_hello(PikaObj* self){
printf("hello!\n");
};
# main.py
import test
print('before run test.hello()')
print('after run test.hello()')
Output.