我现在看一些来自CARLA模拟器(http://carla.org/)的代码。
它使用PosithPython向Python公开许多C++类和成员函数。但我不能从下面几行理解语法。。你知道吗
void export_blueprint() {
using namespace boost::python;
namespace cc = carla::client;
namespace crpc = carla::rpc;
...
class_<cc::ActorBlueprint>("ActorBlueprint", no_init)
.add_property("id", +[](const cc::ActorBlueprint &self) -> std::string {
return self.GetId();
})
.add_property("tags", &cc::ActorBlueprint::GetTags)
.def("contains_tag", &cc::ActorBlueprint::ContainsTag)
.def("match_tags", &cc::ActorBlueprint::MatchTags)
.def("contains_attribute", &cc::ActorBlueprint::ContainsAttribute)
.def("get_attribute", +[](const cc::ActorBlueprint &self, const std::string &id) -> cc::ActorAttribute {
return self.GetAttribute(id);
}) // <=== THESE LINES
.def("set_attribute", &cc::ActorBlueprint::SetAttribute)
.def("__len__", &cc::ActorBlueprint::size)
.def("__iter__", range(&cc::ActorBlueprint::begin, &cc::ActorBlueprint::end))
.def(self_ns::str(self_ns::self))
;
}
下面的代码是什么
.def("get_attribute", +[](const cc::ActorBlueprint &self,
const std::string &id) -> cc::ActorAttribute {
return self.GetAttribute(id);
})
什么意思?看起来python类ActorBlueprint的函数get\u attribute函数是用python接口传递的新参数新定义的(重写)。我几乎可以肯定,但是有关于这个语法的文档吗?我在https://www.boost.org/doc/libs/1_63_0/libs/python/doc/html/tutorial/index.html里找不到。你知道吗
我会逐项解释的。你知道吗
您可以阅读lambda语法here。 而且
+
在lambda面前对你来说可能会很奇怪。它用于触发到普通旧函数指针的转换。读here。你知道吗lambda
-> cc::ActorAttribute
中的箭头指定返回类型。它也可以用于普通函数和方法。你知道吗这是本机c++语法,不是boost特有的。你知道吗
作为这段代码的结果,python类
ActorBlueprint
的方法get_attribute
将被定义。它将有一个字符串参数,并将执行lambda body所做的操作(在本例中按id返回属性)。你知道吗相关问题 更多 >
编程相关推荐