开发者

Swig-generated Constants in Python

I'm using SWIG to create a Python interface to my C++ class library.

I can't work out how to utilise the constants created by SWIG in Python. I can't even print their value.

For example, both these print statements in Python fail silently...

print CONST1
print rep (CONST1)

In C++, I have this

#define CONST1 0x20000
const int CONST2 = 0x20000; // No different to #define in SWIG-generated code.

If I look at the Python module created by SWIG it has something like this...

CONST1 = _theCPPlibrary.CONST1

Additionally, I tried using the SWIG %constant directive as an experiment (I don't really want to use this if I can avoid it, as it involves duplicating my constants in the SWIG input file). The %co开发者_如何转开发nstant directive also gives the same results.

I'm a C++ programmer, and a noob in Python.


After build, you will get a python source file: theCPPlibrary.py, and a pyd file: _theCPPlibrary.pyd. You must import the python module first:

import theCPPlibrary

CONST1 is defined by #define, it can be access by:

print theCPPlibrary.CONST1

CONST2 is defined by const, is't a global variable, access it by:

print theCPPlibrary.cvar.CONST2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜