Python 参数类型 C++签名
我有两个问题,都与 Python 和 C++ 之间的参数如何混合有关...我有一个 C++ 函数,我从 python 调用它,我的函数接受日期和字符串。
python str
与 C++ 相同吗
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
其次,我的函数需要一个 class boost::gregorian::date
类型的日期,有谁知道我如何给出一个python 中的日期将使用正确的签名读取?
非常感谢帮助!我希望这是一个相当简单的问题,我只是对不同类型的签名不太有经验(这对于我当前链接 Python 和 C++ 的实验来说不是一个好兆头)!
I have 2 questions, both relating to how arguments between Python and C++ mix... I have a function in C++ which I am calling from python, and my function takes dates and strings.
Is a python str
the same as a C++
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
And secondly, my function needs a date of type class boost::gregorian::date
, does anyone know how I can give a date in python that will be read with the correct signature?
Help much appreciated! I hope this is a fairly simple problem, I'm just not very experienced with different types of signatures (which doesn't bode well for my current experimentation into linking Python and C++)!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您正在使用
boost::python
,让我告诉您如何在这些情况下继续操作。您可以提供自己的包装,而不是让boost::python
自动包装您的函数:当然,您需要提供一个转换
boost::python::object
的方法到boost::gregorian::date
对象中。你必须决定如何处理这个问题。例如,您可以假设参数是三个整数的序列,或者您可以允许更复杂的方式传递参数,或者定义一个新类来包装公历日期及其所有方法并将其直接公开给 Python。至于你的第一个问题,当使用
boost::python
时,std::string
会自动与 Python 字符串相互转换。Assuming that you are using
boost::python
, let me give you an idea of how to proceed in these cases. Rather than lettingboost::python
automatically wrap your function, you provide your own wrapping:Of course you need to provide a method that converts a
boost::python::object
into aboost::gregorian::date
object. You have to decide how to handle this. For instance, you could assume that the parameter is a sequence of three integers, or you could allow more complex way of passing the parameters, or define a new class that wraps the gregorian date and all of its method and exposes it directly to Python.As for your first question, when using
boost::python
std::string
s are automatically converted to/from Python strings.不可以。为了从 C 或 C++ 调用 Python 函数,您必须使用
Python C API。对于 C++ 字符串,这意味着首先
使用 PyString_FromStringAndSize( 将其转换为 Python 字符串
cppString.data(), cppString.size() ),并传入结果
PyObject.如果你想调用任意函数,你必须
使用
PyImport_Import
加载模块,然后使用PyObject_GetAttrString
获取函数(Python函数是属性,就像
其他一切),然后使用
PyObject_CallObject
调用它,之后转换了所有参数并将它们放入一个元组中(全部通过
Python C API 中的函数)。
No. In order to call Python functions from C or C++, you have to use
the Python C API. In the case of a C++ string, this will mean first
converting it to a Python string, using
PyString_FromStringAndSize(
, and passing in the resultingcppString.data(), cppString.size() )
PyObject. And if you want to call an arbitrary function, you'll have to
use
PyImport_Import
to load the module, thenPyObject_GetAttrString
to get the function (Python functions are attributes, just like
everything else), then use
PyObject_CallObject
to call it, afterhaving converted all of the arguments and put them in a tuple (all via
functions in the Python C API).