在boost::python的类中公开公共结构

2024-05-21 01:44:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python代码上使用这个C++类,用Boosi::Python

/* 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;
    };
};

问题是如何在boostpython中公开这个公共属性

^{pr2}$

Tags: 代码privatepublicallstructareclasshuman
1条回答
网友
1楼 · 发布于 2024-05-21 01:44:56

当类型通过Boost.Python,它们被注入current scope。有些类型,例如用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

交互式Python:

^{pr2}$

相关问题 更多 >