【问题标题】:Set __name__ attribute using boost::python::make_function使用 boost::python::make_function 设置 __name__ 属性
【发布时间】:2019-11-26 12:20:17
【问题描述】:

在使用 make_function 创建 Python 对象后,我需要访问该对象中的函数名。

我试过了:

void foo() {}
boost::python::object function = boost::python::make_function(foo);

// I got "unnamed Boost.Python function"
std::string name = boost::python::extract<std::string>(function.attr("__name__"));

// I got "attribute '__name__' of 'Boost.Python.function'"
function.attr("__name__") = "foo";

是否可以使用 make_function 设置“__name__”属性?

【问题讨论】:

  • 我不是通过使用 make_function butt PyMethoDef 解决了这个问题。例如,PyMethodDef * def = new PyMethodDef(); def-&gt;ml_name="foo"; ...

标签: python c++ boost


【解决方案1】:

我经常只是用我想要的任何属性创建一个真正的 Python 函数,并让它调用我用 C++ 实现的内部函数(以_ 开头,表示它不是公共的)。

然后你可以做任何你想做的事情,help(foo) 和类似的东西会看起来更熟悉,因为你的可见函数名称只是一个普通的 Python 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2016-10-16
    • 1970-01-01
    • 2014-06-18
    • 2016-12-07
    • 2012-06-01
    相关资源
    最近更新 更多