首页 > 解决方案 > 如何使用 pybind11 传递 Python 的关键字参数?

问题描述

给定这个函数:__init__(username, password, **kwargs)使用这些关键字参数:

我想用 pybind11 将 python 嵌入到我的 C++ 应用程序中。如何传递关键字参数?我是这么远:

#include <pybind11/embed.h> // everything needed for embedding
#include <iostream>
namespace py = pybind11;

int main()
{
    py::scoped_interpreter guard{}; // start the interpreter and keep it alive

    py::module calc = py::module::import("calc");
    py::object result = calc.attr("__init__")("IGname", "IGpassword");

    int i;
    std::cin >> i;
}

标签: pythonc++embedpybind11

解决方案


我找到了正确的文档:https ://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html

我无法测试它,因为我遇到了一些其他问题,但那是去的地方。


推荐阅读