pybind11 绑定输入输出参数
我的 C++ 代码
void test(std::string & data) {
data += " github";
}
PYBIND11_MODULE(example11, m) {
m.def("test", [](std::reference_wrapper<std::string> w) {
test(w.get());
});
}
我的 python 代码
import example11
def my_test():
data = "hello"
example11.test(data)
print("python: {}".format(data))
if __name__ == '__main__':
my_test()
我期望的是我得到了 hello github,但是得到了 hello,亲爱的朋友们,我的代码有问题吗?
my C++ code
void test(std::string & data) {
data += " github";
}
PYBIND11_MODULE(example11, m) {
m.def("test", [](std::reference_wrapper<std::string> w) {
test(w.get());
});
}
my python code
import example11
def my_test():
data = "hello"
example11.test(data)
print("python: {}".format(data))
if __name__ == '__main__':
my_test()
what i expect is i got hello github, however hello is gotten, is there something wrong in my code, dear guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在Python中使用输出参数,请参阅 官方文档。只有可变类型(
str
除外)可以就地更改。即使对于可变类型,由于 pybind11。要解决您的问题,请返回修改后的值。
在Python调用中
You can't use output parameter in Python, see detailed explaination in the official doc. Only mutable types(
str
exclued) can be changed in-place. Even for mutable types, you still can't get modified result since the restriction of pybind11.To fix your issue, return the modified value.
in Python call