添加成员函数升压Python类事后? [英] Add member functions to a Boost Python class after the fact?

查看:79
本文介绍了添加成员函数升压Python类事后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在升压Python,可以暴露一个C ++类到Python这样的:

In Boost Python, you can expose a C++ class to Python like this:

object obj = class_<MyClass>("MyClass")
    .def("SomeFunc", &MyClass::SomeFunc)
    ;

假设MyClass的也有一个功能SomeOtherFunc。有没有办法在BOOST_PYTHON_MODULE code暴露SomeOtherFunc到Python以后?我试着用返回的对象,但它没有产权清晰度。我也尝试MyClass的实例再次曝光,但只是覆盖了第一个。基本上我正在寻找这样的事情:

Let's say MyClass also has a function SomeOtherFunc. Is there a way to expose SomeOtherFunc to Python later on in the BOOST_PYTHON_MODULE code? I tried using the returned object, but it has no property def. I also tried instantiating the exposing of MyClass again, but that just overwrote the first one. Essentially I'm looking for something like this:

obj.def("SomeOtherFunc", &MyClass::SomeOtherFunc)

这是我试图解决更大的问题是,我有很多的,从模板基类继承类,我试图找到一种方式来揭露他们到Python以干净的方式。到目前为止,我做了一个模板函数,它在类和它的模板参数,构建必要的class_电话,但我可能要修改暴露后级的定义,所以我不知道该怎么做

The larger problem that I'm trying to solve is that I have a number of classes that inherit from a templated base class, and I'm trying to find a way to expose them to Python in a clean way. So far, I've made a template function that takes in the class and its template parameter and constructs the necessary class_ calls, but I might want to modify the definition of the exposed class later, so I'm not sure how to do that.

奖励积分,如果有人能解释一下到底是什么高清在做什么,为什么我不需要DEFS之间的逗号。

Bonus points if someone can explain exactly what "def" is doing and why I don't need commas in between defs.

推荐答案

OBJ 没有名为 DEF ,但类_ 一样。 类_ 继承对象,这就是为什么分配的工作,但你不知道的带有做到这一点。以下工作正常:

obj doesn't have a member function named def, but class_ does. class_ inherits from object, which is why that assignment works, but you don't have to do that. The following works fine:

auto obj = class_<MyClass>("MyClass")
    .def("SomeFunc", &MyClass::SomeFunc)
;

// now obj is an instance of py::class_<MyClass> instead of py::object
obj.def("SomeOtherFunc", &MyClass::SomeOtherFunc);

这是你从来没有命名的事实类_ 实例可以从事实,这是一个简单的构造分散。你可能也写上如下:

The fact that you never name the class_ instance may distract from the fact that this is a simple constructor. You could have also written the above as:

class_<MyClass> obj("MyClass");
obj.def("SomeFunc", &MyClass::SomeFunc);
obj.def("SomeOtherFunc", &MyClass::SomeOtherFunc);

您不必一气呵成定义的一切。

You don't have to define everything in one go.

奖励积分,如果有人能解释一下到底是什么 DEF 在做什么,为什么我不需要DEFS之间的逗号。

Bonus points if someone can explain exactly what def is doing and why I don't need commas in between defs.

DEF 是类模板成员函数类_ 。它有一个一堆重载的,所有这些都返回引用自:

def is a member function on the class template class_. It has a bunch of overloads, all of which returns a reference to self:

template <class Init>         class_& def(Init );
template <class F>            class_& def(char const*, Fn );
template <class Fn, class A1> class_& def(char const*, Fn, A1 );
// etc.

这就是为什么你可以把它们连。每次通话单独设置了内部机制,以公开该功能蟒蛇,只是返回 *本。我不知道你需要什么逗号的。

That's why you can chain them. Each call independently sets up the internal machinery to expose that function to python and just returns *this. I don't know what you would need commas for.

这篇关于添加成员函数升压Python类事后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆