在类中公开公共结构以 boost::p ython
expose public struct inside a class for boost::python
我想在python代码上使用这个C++类,并带有boost::p ython
/* creature.h */
class Human {
private:
public:
struct emotion {
/* All emotions are percentages */
char joy;
char trust;
char fear;
char surprise;
char sadness;
char disgust;
char anger;
char anticipation;
char love;
};
};
问题是如何在 boost-python 中公开这个公共属性
namespace py = boost::python;
BOOST_PYTHON_MODULE(example)
{
py::class_<Human>("Human");
// I have not idea how put the public struct here
}
当类型通过 Boost.Python 公开时,它们被注入到当前作用域中。 某些类型(例如随 class_
引入的类型)可以用作当前作用域。
下面是一个完整的注释示例:
#include <boost/python.hpp>
struct Human
{
struct emotion
{
char joy;
// ...
};
};
BOOST_PYTHON_MODULE(example) // set scope to example
{
namespace python = boost::python;
{
python::scope in_human = // define example.Human and set
python::class_<Human>("Human"); // scope to example.Human
python::class_<Human::emotion>("Emotion") // define example.Human.Emotion
.add_property("joy", &Human::emotion::joy)
;
} // revert scope, scope is now
} // example
交互式蟒蛇:
>>> import example
>>> e = example.Human.Emotion
>>> e
<class 'example.Emotion'>
>>> hasattr(e, 'joy')
True
相关文章:
- 相当于 pybind11 中的 boost::p ython py::scope().attr()
- 传递 boost::p ython::numpy::ndarray 作为 boost::p ython 函数的(默认或非
- boost::p ython:PyErr_Fetch始终返回 NULL 回溯
- 如何通过 boost::p ython 重命名构造函数的关键字参数
- 链接问题 boost::p ython::numpy.
- 在 boost::p ython 中的 python 对象中运行 python 代码
- 你如何在 boost::p ython 中"from __future__ import division"?
- 使用 boost::p ython 将数据缓冲区放入C++中
- 如何返回包含 PyObject* 的 boost::p ython::元组?
- 如何正确访问结构值并将其传递给函数 - boost::p ython
- Boost::P ython,将元组转换为Python有效,矢量<tuple>不
- 如何使用 boost::p ython 创建和使用 Python 对象的实例
- 使用 boost::p ython,如何将结构体向量作为字典列表返回给 Python
- 使用 boost::p ython 从C++创建 python collections.namedtuple
- 如何使用 boost::p ython 从 Python 类型中提取包装的C++类型
- Boost::p ython 静态方法返回类的实例
- boost::p ython 纯虚拟基类,具有静态工厂构造函数和 std::unique_ptr
- 在类中公开公共结构以 boost::p ython
- 如何使用Boost::P ython打印到Python终端
- 如何使用 boost::p ython::迭代器与return_internal_reference