I have a simple enum in C in myenum.h:
enum MyEnum {
ONE,
TWO,
THREE
};
The problem is that when I map this to Python, I can only access the enum through the module name, not through MyEnum. So the values ONE, TWO, THREE are included with any other functions I define, instead of being contained with MyEnum.
My api.i file is:
%module api
%{
#include "myenum.h"
%}
%include "myenum.h"
I generate with SWIG
swig -builtin -python api.i
And import it into Python
import _api
And now I have to use the enum values from the _api module:
_api.ONE
_api.TWO
_api.THREE
While I want to use them like
_api.MyEnum.ONE
_api.MyEnum.TWO
_api.MyEnum.THREE
Does anyone know how I can accomplish this?
There is a SWIG feature nspace that would do want you want, but unfortunately it isn't supported for Python yet. I've always had to define the enum in a struct for it to show up in the manner you desire in SWIG. Example:
%module tmp
%inline %{
struct MyEnum {
enum { A,B,C };
};
%}
Result:
>>> import tmp
>>> tmp.MyEnum.A
0
>>> tmp.MyEnum.B
1
>>> tmp.MyEnum.C
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With