mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
656 B
656 B
icon | label | order |
---|---|---|
dot | Call Python function | 70 |
pkpy uses a variant of the Vectorcall protocol (PEP 590).
There are 2 methods for calling a python function.
PyObject* VM::call(PyObject* obj, ...)
PyObject* VM::call_method(PyObject* obj, StrName name, ...)
For example, to create a dict
object,
PyObject* tp = vm->builtins->attr("dict");
PyObject* obj = vm->call(tp); // this is a `dict`
And set a key-value pair,
vm->call_method(obj, "__setitem__", VAR("a"), VAR(5));
PyObject* ret = vm->call(obj, "__getitem__", VAR("a"));
std::cout << CAST(int, ret) << std::endl; // 5