typedef struct _Names
{
int a;
int b;
int c;
} Names;
typedef struct _values
{
Names names;
}values;
我拥有的绑定代码是
py::class_<Names>(m, "Names")
.def(py::init<>())
.def_readwrite("a", &Names::a)
.def_readwrite("b", &Names::b)
.def_readwrite("c", &Names::c);
py::class_<values>(m, "values")
.def(py::init<>())
.def_readwrite("names", &values::names);
但是我无法访问names.a或names.b或names.c。
如何绑定具有名称结构的名称?
答案 0 :(得分:1)
欢迎来到!
哼,应该可以。我的猜测是您的python代码未正确实例化类values
。
我有
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py=pybind11;
typedef struct _Names
{
int a;
int b;
int c;
} Names;
typedef struct _values
{
Names names;
} values;
// Wrapping code
PYBIND11_MODULE(wrapper, m) {
py::class_<Names>(m, "Names")
.def(py::init<>())
.def_readwrite("a", &Names::a)
.def_readwrite("b", &Names::b)
.def_readwrite("c", &Names::c);
py::class_<values>(m, "values")
.def(py::init<>())
.def_readwrite("names", &values::names);
}
如果我将其编译(如果需要说明,请参见example 1 in this github repo)到名为wrapper
的文件中,则可以从python中将其用作
import wrapper
b = wrapper.values() # <-- don't forget the parenthesis !
b.names.a = 30
print(b.names.a) # prints 30
我猜你没有在wrapper.values
后面加上括号,导致(在我的情况下)b
包含类定义而不是它的实例。
希望它会有所帮助:)