我无法将类方法的输入(输入:单独类的特定实例)返回到Python.绑定被编译,我可以使用在Python中生成的模块.但是,类方法应该返回它所允许的相同实例(经过一些处理之后).

使用Obstacle类作为输入.ObstacleProcess类有一个处理输入(Obstacle的实例)的方法(Python:__call__/C++:operator_py).下面的Python代码显示返回Obstacle的不同实例:

import example

obstacle_1 = example.Obstacle()
obstacle_2 = example.Obstacle()

obstacles = [obstacle_1, obstacle_2]
print(obstacles)

params = example.Params()
obstacle_process = example.ObstacleProcess(params)
obstacles = obstacle_process(obstacles)
print(obstacles)

第一个打印返回:[<example.Obstacle object at 0x7fb65271e1b0>, <example.Obstacle at 0x7fb652735070>],而第二个打印返回:[<example.Obstacle at 0x7fb652734670>, <example.Obstacle object at 0x7fb652735230>].

这不是期望的输出,因为obstacle_1最初位于0x7fb65271e1b0,而在operator()/__call__调用之后,它位于0x7fb652734670.我希望obstacle_1保留其初始地址0x7fb65271e1b0,即使在另一个类(ObstacleProcess)处理了obstacle_1之后也是如此.

以下代码显示源代码绑定了pybind11:

// pybind11 binding
py::class_<Obstacle, std::shared_ptr<Obstacle>>(m, "Obstacle")
    .def(py::init<>());

py::class_<ObstacleProcess>(m, "ObstacleProcess")
    .def(py::init<
        const Params&>()
    )
    .def("__call__", &ObstacleProcess::operator_py<Params>, py::return_value_policy::reference);

下一块显示了如何在源代码中实现operator_py:

template <Params>
std::vector<Obstacle>& operator_py(
    std::vector<Obstacle>& obstacles,
    const Params &parameters
)
{

    ...

    return obstacles
}

我试过用std::shared_ptr<Obstacle>和不用std::shared_ptr<Obstacle>.当前实现的结果与根本不使用shared_ptr的结果相同,因此我实现shared_ptr的方式有问题.我try 使用PYBIND11_MAKE_OPAQUE(std::shared_ptr<std::vector<Obstacle>>);,但我的实现并没有改变结果.

我没有试过用pybind11 — smart_holder branch,也许这次要用这个 twig 呢?

推荐答案

根据@Danmašek和@n.m.will seey‘allonReddit的 comments ,对operator_py()%进行这些更改可以解决问题中的问题:

template <class Params>
std::vector<std::shared_ptr<Obstacle>>& operator_py(
    std::vector<std::shared_ptr<Obstacle>> &obstacles,
    const Params &params
    )
    {
    for (auto& obstacle : obstacles)
        {
             obstacle->someObstacleClassMethod()
             someObstacleProcessMethod(obstacles, params)
        }   
    return obstacles;
    }

实施后,获得了所需的输出:

import example

obstacle_1 = example.Obstacle()
obstacle_2 = example.Obstacle()

obstacles = [obstacle_1, obstacle_2]
print(obstacles)

params = example.Params()
obstacle_process = example.ObstacleProcess(params)
obstacles = obstacle_process(obstacles)
print(obstacles)

# output:
[<example.Obstacle object at 0x7f709bdc2430>, <example.Obstacle object at 0x7f709b12a830>]
[<example.Obstacle object at 0x7f709bdc2430>, <example.Obstacle object at 0x7f709b12a830>]

Python相关问答推荐

修剪Python框架中的尾随NaN值

在Docker中运行HAProxy时无法获得503服务

使用Python C API重新启动Python解释器

按 struct 值对Polars列表[struct[]]排序

自定义新元未更新参数

Tkinter滑动条标签.我不确定如何删除滑动块标签或更改其文本

无法使用python.h文件; Python嵌入错误

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

从管道将Python应用程序部署到Azure Web应用程序,不包括需求包

使用GEKKO在简单DTE系统中进行一致初始化

Python多处理:当我在一个巨大的pandas数据框架上启动许多进程时,程序就会陷入困境

使用新的类型语法正确注释ParamSecdecorator (3.12)

滚动和,句号来自Pandas列

如何在Python中并行化以下搜索?

基于字符串匹配条件合并两个帧

如何设置视频语言时上传到YouTube与Python API客户端

导入...从...混乱

Python Pandas获取层次路径直到顶层管理

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

搜索按钮不工作,Python tkinter