【问题标题】:how to send structure as parameter in python如何在python中将结构作为参数发送
【发布时间】:2013-11-12 10:11:13
【问题描述】:

我正在使用 boost python 来实现 C++ 和 Python 之间的互操作性。我在 c++ 中有枚举和结构,并且在 c++ 中有可以将这些结构作为参数的函数。我可以通过使用 boost python 在 python 中访问这个函数,但我不知道如何在 python 中将结构作为参数发送。 Set 是 c++ 中的函数,可以将结构作为参数获取。所以在 python 中,我如何将此结构作为参数发送。我能够在 python 中获得此功能,但无法将发送结构作为参数发送。感谢您的帮助。

c++结构如下:

enum days
{
  friday,
  saturday
};
struct example
 {
    days m_day;
    std: string m_value;
 };

Class Base
 {
   public:
   void Set(example& Result) = 0;
  }
class Derived
{
  public:
  void Set(example& Result) 
  {
    Result.m_day = friday;
   }

【问题讨论】:

  • 不,我想将结构作为 pyrameter 从 python 发送到 c++。
  • 也许这对你有帮助? send-receive-cstruct-using-python
  • 如何从 python 发送空结构,它可以评估 c++ 的 Structurevalue?无法从 python 发送结构
  • 通过boost::python::enumboost::python::class_ 公开枚举和结构。 Boost.Python 将处理结构和相关 Python 类型之间的转换。
  • 问题是我如何从 python 发送空结构,它可以访问 c++ 中的结构。

标签: c++ python boost-python


【解决方案1】:

您必须像这样公开结构示例,然后在 python 中创建此结构的变量并将此变量作为参数发送。

Class_<example> (“example”)
.def_readwrite(“m_day” , & example:: day)
.def_readwrite(“m_value” , & example:: m_value)
;

希望这会有所帮助..

【讨论】:

    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    相关资源
    最近更新 更多