我想从另一个文件夹导入一个类。在另一个 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/