python - 使用 Boost.Python 从 X 导入 Y

标签 python c++ python-2.7 boost boost-python

我想从另一个文件夹导入一个类。在另一个 python 脚本中,我会做

从 Base.Derived 导入类

但是我无法弄清楚如何使用 Boost.Python 执行此操作。该库提供了 import.hpp,它可以让你做这样的事情

object module = import("Base.Derived");

但是python中的等价物是

import Base.Derived

最终目标是将实例化的 python 对象转换为 Base 指针,因此首选使用 Boost.Python。理想情况下,代码看起来像这样

object module = some form of "from Base.Derived import Class"

// Get a C++ pointer of the derived python class.
object derived = module.attr("Class")();
Card* card = extract< Card* >(derived);

最佳答案

“点分”表示法中的每个名称都是其父项的属性。你的最后一段代码几乎是正确的(尽管我怀疑名字有些混淆):

boost::python::object Class = boost::python::import("Base.Derived").attr("Class");
boost::python::object class_instance = Class();

关于python - 使用 Boost.Python 从 X 导入 Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44866181/

相关文章:

python - ncpol2sdpa python工具中出现错误 'OSError: [Errno 2] No such file or directory'的原因是什么?

python - 使用bs4提取html文件中的文本

python - 如何使用 Python 运行 Google gsutil

python - 无法在Python中安装Tensorflow

python - 按对象或两个 float 索引 python dict

c++ - 读取 MAC 地址时文件读取异常

c++ - 使用 flex 和 bison 进行乘法解析

python矩阵乘法: how to handle very large matrices?

c++ - C++ 中 32 位和 64 位 Mach-O 二进制/可执行格式的解析器

python - 使用 for 循环从字典中删除项目