【问题标题】:Convert python dict to a structure using ctypes使用 ctypes 将 python dict 转换为结构
【发布时间】:2017-11-14 05:28:53
【问题描述】:

我有这样的结构:

struct Person {
  std::string name;
  int age;
  std::string title;
  float income;
};

在 python 方面,我有一个这样的字典:

person = { 'name':'Alex', 
           'age':36, 
           'title':'programmer', 
           'income':13435.40
          }

现在我想使用 ctypes 将 Python dict 转换为 C++ 程序。完成这项任务有一个愚蠢的方法是我们可以编写 4 个 c 风格的函数来独立设置这四个元素。我们如何在一个函数中完成这个任务,它可以解决不同的数据类型(字符串、整数、浮点数)?

【问题讨论】:

  • string 是什么?如果它是std::string,它就是一个 C++ 程序。没有名为 C/C++ 的野兽。
  • 是的,它是 std::string
  • 那么你应该已经适当地标记了。这需要 C++ 专业知识。

标签: python c++ ctypes


【解决方案1】:

ctypes 无法单独处理 C++。

SWIG can 虽然这相当于为 C++ 代码编写 C 包装器。它需要构建原生代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 2019-10-08
    • 2013-06-15
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多