我有问题使用Boost-Python包装Enum for Python。使用Boost-Python包装枚举
起初,我打算做类似的东西在的try-catch以下(我已经插入了我的整个代码如下)声明:
main_namespace["Motion"] = enum_<TestClass::Motion>("Motion")
.value("walk", TestClass::walk)
.value("bike", TestClass::bike)
;
一切都很好,并编制已完成。在运行时,我得到这个错误(这是没有意义的我):
AttributeError: 'NoneType' object has no attribute 'Motion'
后来我决定写在我的代码使用BOOST_PYTHON_MODULE一个Python模块。 初始化Python解释器后,我想马上使用这个模块,但不知道如何(?)。以下是我的整个代码:
#include <boost/python.hpp>
#include <iostream>
using namespace std;
using namespace boost::python;
BOOST_PYTHON_MODULE(test)
{
enum_<TestClass::Motion>("Motion")
.value("walk", TestClass::walk)
.value("bike", TestClass::bike)
;
}
int main()
{
Py_Initialize();
try
{
object pyMainModule = import("__main__");
object main_namespace = pyMainModule.attr("__dict__");
//What previously I intended to do
//main_namespace["Motion"] = enum_<TestClass::Motion>("Motion")
// .value("walk", TestClass::walk)
// .value("bike", TestClass::bike)
//;
//I want to use my enum here
//I need something like line below which makes me able to use the enum!
exec("print 'hello world'", main_namespace, main_namespace);
}
catch(error_already_set const&)
{
PyErr_Print();
}
Py_Finalize();
return 0;
}
任何有用的知道包装和使用Python中的枚举将不胜感激! 在此先感谢
你的演示完美地契合了答案。感谢那。在你强制的范围内,这对我来说是一种骗术。这实际上解决了我的问题。我想问一个关于python范围的简单解释。其实我无法理解范围和命名空间之间的区别。 –
@NOVIN:['scope'](http://www.boost.org/doc/libs/1_54_0/libs/python/doc/v2/scope.html#introduction)是一个Boost.Python结构,用于表示名称空间将包含新包装的类和函数。另外,如果你发现答案是有益的,你可以考虑[upvoting或accept](http://meta.stackoverflow.com/help/someone-answers)它。 –