【问题标题】:Import and import as in boost::python在 boost::python 中导入和导入
【发布时间】:2023-01-26 19:05:10
【问题描述】:

我正在开发一个项目,其中大部分代码是 C++ 代码,一些是 python 代码。

有没有办法从 C++ 调用 import xxx 和/或 import xxx as x

我会期待这样的事情:

auto other_mod = boost::python::import("the_other_module");

BOOST_PYTHON_MODULE(pystuff)
{
    boost::python::module_<other_mod>("wrapping_name"); // I just invented this
}

然后在 python 中能够:

from pystuff import wrapping_name as wn

wn.someFunction()

请注意,我不想在 python 中执行此操作

import pystuff
import the_other_module

the_other_module 中的对象与 pystuff 中的对象具有相似的目标和依赖关系,因此我不希望用户有一个没有另一个。

我也知道我可以接受每个对象来自the_other_module,我想公开和包装,但我不想一一做。

【问题讨论】:

    标签: python c++ boost bind


    【解决方案1】:

    我不是那么远,我只是做了:

    auto other_mod = boost::python::import("the_other_module");
    
    BOOST_PYTHON_MODULE(pystuff)
    {
        boost::python::scope().attr("wrapping_name") = other_mod;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多