简体   繁体   English

从X使用Boost.Python导入Y

[英]from X import Y with Boost.Python

I want to import a class from another folder. 我想从另一个文件夹导入一个类。 In another python script I would do 在另一个python脚本中,我会做

from Base.Derived import Class

However I can't figure out how to do this with Boost.Python. 但是我不知道如何使用Boost.Python做到这一点。 The library provides import.hpp which lets you do something like this 该库提供import.hpp,使您可以执行以下操作

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

But the equivalent in python is 但是python中的等效项是

import Base.Derived

The end goal is to get an instantiated python object into a Base pointer, so using Boost.Python is preferred. 最终目标是将实例化的python对象获取到Base指针中,因此首选使用Boost.Python。 Ideally the code would look something like this 理想情况下,代码应如下所示

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);

Each name in a "dotted" notation is an attribute of its parent. 每个带有“点分”符号的名称都是其父级的属性。 And your last piece of code is almost correct (although, I suspect some mix-up with names): 您的最后一段代码几乎是正确的(尽管我怀疑名称混为一谈):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM