Boost.python 重载 numpy 数组和 python 列表的构造函数
给定一个使用 Boost.Python 公开的 C++ 类,如何公开两个构造函数:
- 一个采用 numpy 数组,
- 另一个采用 python 列表?
Given a C++ class exposed with Boost.Python, how do I expose two constructors:
- one that takes a numpy array, and
- another that takes a python list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 100% 理解你的意思,但我假设你想要一个构造函数接受一个 Python 列表,另一个构造函数接受一个 numpy 数组。有几种方法可以解决这个问题。最简单的方法是使用 make_constructor 函数并重载它:
您可以更聪明,在构造函数中使用任意类型/数量的参数。这需要一点巫毒才能完成。请参阅http://wiki.python.org/moin/boost。 python/HowTo#A.22Raw.22_constructor 用于将原始函数定义公开为构造函数的方法。
I'm not a 100% on what you mean, but I'm assuming that you want to have a constructor taking a Python list and another one taking a numpy array. There are a couple of ways to go about this. The easiest way is by using the make_constructor function and overloading it:
You can be even more clever and use an arbitrary type/number of arguments in your constructor. This requires a bit of voodoo to accomplish. See http://wiki.python.org/moin/boost.python/HowTo#A.22Raw.22_constructor for a way to expose a raw function definition as a constructor.