<i id='fzfJY'><tr id='fzfJY'><dt id='fzfJY'><q id='fzfJY'><span id='fzfJY'><b id='fzfJY'><form id='fzfJY'><ins id='fzfJY'></ins><ul id='fzfJY'></ul><sub id='fzfJY'></sub></form><legend id='fzfJY'></legend><bdo id='fzfJY'><pre id='fzfJY'><center id='fzfJY'></center></pre></bdo></b><th id='fzfJY'></th></span></q></dt></tr></i><div id='fzfJY'><tfoot id='fzfJY'></tfoot><dl id='fzfJY'><fieldset id='fzfJY'></fieldset></dl></div>
  • <tfoot id='fzfJY'></tfoot>
    • <bdo id='fzfJY'></bdo><ul id='fzfJY'></ul>

        <legend id='fzfJY'><style id='fzfJY'><dir id='fzfJY'><q id='fzfJY'></q></dir></style></legend>

        <small id='fzfJY'></small><noframes id='fzfJY'>

        从 C++ 调用 Python 函数

        时间:2023-10-17

            <tbody id='X8m2k'></tbody>
          <i id='X8m2k'><tr id='X8m2k'><dt id='X8m2k'><q id='X8m2k'><span id='X8m2k'><b id='X8m2k'><form id='X8m2k'><ins id='X8m2k'></ins><ul id='X8m2k'></ul><sub id='X8m2k'></sub></form><legend id='X8m2k'></legend><bdo id='X8m2k'><pre id='X8m2k'><center id='X8m2k'></center></pre></bdo></b><th id='X8m2k'></th></span></q></dt></tr></i><div id='X8m2k'><tfoot id='X8m2k'></tfoot><dl id='X8m2k'><fieldset id='X8m2k'></fieldset></dl></div>
          <tfoot id='X8m2k'></tfoot>
            <bdo id='X8m2k'></bdo><ul id='X8m2k'></ul>

            <small id='X8m2k'></small><noframes id='X8m2k'>

                • <legend id='X8m2k'><style id='X8m2k'><dir id='X8m2k'><q id='X8m2k'></q></dir></style></legend>
                  本文介绍了从 C++ 调用 Python 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 C++ 实现调用 Python 函数.我以为可以通过函数指针来实现,但似乎不太可能.我一直在使用 boost.python 来完成这个.

                  I am trying to achieve call Python functions from C++. I thought it could be achieved through function pointers, but it does not seem to be possible. I have been using boost.python to accomplish this.

                  假设在 Python 中定义了一个函数:

                  Say there is a function defined in Python:

                  def callback(arg1, arg2):
                      #do something
                      return something
                  

                  现在我需要将此函数传递给 C++,以便可以从那里调用它.我如何使用 boost.python 在 C++ 端编写代码来实现这一点?

                  Now I need to pass this function to C++, so that it can be called from there. How do I write the code on C++ side using boost.python to achieve this?

                  推荐答案

                  如果它可能有任何名称:

                  If it might have any name:

                  将它传递给一个接受 boost::python::object 的函数.

                  Pass it to a function that takes a boost::python::object.

                  bp::object pycb; //global variable. could also store it in a map, etc
                  void register_callback(bp::object cb)
                  {
                        pycb = cb;
                  }
                  

                  如果它位于具有一致名称的单个已知命名空间中:

                  If it is in a single known namespace with a consistent name:

                  bp::object pycb = bp::scope("namespace").attr("callback");
                  

                  bp::object 定义了 operator(),所以你可以像调用任何函数一样调用它

                  bp::object has operator() defined, so you call it just like any function

                  ret = pycb()
                  

                  这篇关于从 C++ 调用 Python 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 的面向对象回调? 下一篇:如何使用 C++ 成员函数作为 C 框架的回调函数

                  相关文章

                  <legend id='xPDyc'><style id='xPDyc'><dir id='xPDyc'><q id='xPDyc'></q></dir></style></legend>

                      <small id='xPDyc'></small><noframes id='xPDyc'>

                      <tfoot id='xPDyc'></tfoot>
                      <i id='xPDyc'><tr id='xPDyc'><dt id='xPDyc'><q id='xPDyc'><span id='xPDyc'><b id='xPDyc'><form id='xPDyc'><ins id='xPDyc'></ins><ul id='xPDyc'></ul><sub id='xPDyc'></sub></form><legend id='xPDyc'></legend><bdo id='xPDyc'><pre id='xPDyc'><center id='xPDyc'></center></pre></bdo></b><th id='xPDyc'></th></span></q></dt></tr></i><div id='xPDyc'><tfoot id='xPDyc'></tfoot><dl id='xPDyc'><fieldset id='xPDyc'></fieldset></dl></div>
                        <bdo id='xPDyc'></bdo><ul id='xPDyc'></ul>