# Call Python function

pkpy uses a variant of the Vectorcall protocol (PEP 590).

You can use call to invoke any python callable object, including functions, methods, classes, etc. For methods, call_method can be used.

  • PyObject* VM::call(PyObject* obj, ...)
  • PyObject* VM::call_method(PyObject* obj, StrName name, ...)

# Exmaple

Let's create a dict object and set a key-value pair, which equals to the following python snippet.

obj = {}        # declare a `dict`
obj["a"] = 5    # set a key-value pair
print(obj["a"]) # print the value

First, create an empty 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));

And get the value,

PyObject* ret = vm->call_method(obj, "__getitem__", VAR("a"));
std::cout << CAST(i64, ret) << std::endl;