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 %constant 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
链接地址: http://www.djcxy.com/p/48330.html

上一篇: 使用SWIG在Python中访问C ++ typedef

下一篇: 在Python中生成常量