首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在C++中使用pybind11将Python类表示为std::variant的类型

在C++中使用pybind11将Python类表示为std::variant的类型
EN

Stack Overflow用户
提问于 2020-12-11 12:24:06
回答 1查看 660关注 0票数 2

下面的工作示例将由int64_t和float组成的变量类型的向量返回给Python。其意图(由注释的代码行**所示)是通过允许在C++中构造的Decimal (python类)以相同的结构传递回Python,从而向其添加进一步的功能。

代码语言:javascript
复制
#include <pybind11/pybind11.h>
#include <vector>
#include <variant>
#include <string>
#include <pybind11/stl.h>
#include <pybind11/complex.h>
#include <pybind11/functional.h>
#include <pybind11/chrono.h>
namespace py = pybind11;

// Importing Decimal class, as shown here
// https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html?highlight=Decimal(#accessing-python-libraries-from-c
py::object Decimal = py::module_::import("decimal").attr("Decimal");

//typedef std::variant<float, int64_t, Decimal> Listtypes;  // **
typedef std::variant<float, int64_t> Listtypes;

std::vector<ListTypes> returnList() {

    std::vector<ListTypes> list(3);
    
    int64_t integerVal = 987654321;
    float floatVal = 1.01;

    // Constructing python object, as shown here 
   //https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html#callingpythonfunctions
    py::object pi = Decimal("3.14159");
    
    list[0] = integerVal;
    list[1] = floatVal;
    //list[2] = pi;        // **

    return list;
}

PYBIND11_MODULE(pBind, m) {
    m.def("returnList", &returnList, "Function to return list of differing types");
}

因此,是否可以将Decimal表示为std::variant的一部分,以便将其传递回带有向量的Python,或者解决方案不那么简单?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-14 20:25:36

您不能只将pi变量添加到std::variant向量中,因为它的类型是py::object。您可以将它添加到您的ListTypes中,因此只需更改行

typedef std::variant<float, int64_t, py::object> Listtypes;

但是您在Python端的列表将是[987654321, 1.0099999904632568, Decimal(3.14159)]

我认为您宁愿使用潘丁提供的铸件来转换pi变量来浮动,这样您的代码就会变成

代码语言:javascript
复制
#include <pybind11/pybind11.h>
#include <vector>
#include <variant>
#include <string>
#include <pybind11/stl.h>
#include <pybind11/complex.h>
#include <pybind11/functional.h>
#include <pybind11/chrono.h>
namespace py = pybind11;

// Importing Decimal class, as shown here
// https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html?highlight=Decimal(#accessing-python-libraries-from-c
py::object Decimal = py::module_::import("decimal").attr("Decimal");

//typedef std::variant<float, int64_t, Decimal> Listtypes;  // **
typedef std::variant<float, int64_t> Listtypes;

std::vector<ListTypes> returnList() {

    std::vector<ListTypes> list(3);
    
    int64_t integerVal = 987654321;
    float floatVal = 1.01;

    // Constructing python object, as shown here 
   //https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html#callingpythonfunctions
    py::object pi = Decimal("3.14159");
    
    list[0] = integerVal;
    list[1] = floatVal;
    list[2] = pi.cast<float>();        // we cast to float

    return list;
}

PYBIND11_MODULE(pBind, m) {
    m.def("returnList", &returnList, "Function to return list of differing types");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65251326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档