Python 参数类型 C++签名

发布于 2024-12-11 23:57:13 字数 444 浏览 0 评论 0原文

我有两个问题,都与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2024-12-18 23:57:13

假设您正在使用 boost::python,让我告诉您如何在这些情况下继续操作。您可以提供自己的包装,而不是让 boost::python 自动包装您的函数:

    namespace {

        void Foo_methodUsingDate( Foo* pointer, const object& dateObject )
        {
            boost::gregorian::date date = convertFromPython( dateObject );

            pointer->methodUsingDate( date );
        }
    }

    void wrap_Foo()
    {
        class_< Foo >( "Foo" )
            .def( "bar",
                & Foo::bar
                )
            .def( "foobar",
                & Foo::foobar
                )
            .def( "methodUsingDate",
                & Foo_methodUsingDate
                )
            ;
    }

当然,您需要提供一个转换 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 letting boost::python automatically wrap your function, you provide your own wrapping:

    namespace {

        void Foo_methodUsingDate( Foo* pointer, const object& dateObject )
        {
            boost::gregorian::date date = convertFromPython( dateObject );

            pointer->methodUsingDate( date );
        }
    }

    void wrap_Foo()
    {
        class_< Foo >( "Foo" )
            .def( "bar",
                & Foo::bar
                )
            .def( "foobar",
                & Foo::foobar
                )
            .def( "methodUsingDate",
                & Foo_methodUsingDate
                )
            ;
    }

Of course you need to provide a method that converts a boost::python::object into a boost::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::strings are automatically converted to/from Python strings.

2024-12-18 23:57:13

不可以。为了从 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(
cppString.data(), cppString.size() )
, and passing in the resulting
PyObject. And if you want to call an arbitrary function, you'll have to
use PyImport_Import to load the module, then PyObject_GetAttrString
to get the function (Python functions are attributes, just like
everything else), then use PyObject_CallObject to call it, after
having converted all of the arguments and put them in a tuple (all via
functions in the Python C API).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文