我的C代码定义了一个常量,我试图添加使用这个常量的python代码(在pythoncode块中),因为某种原因这是行不通的。
演示.i文件:
%module test
%{
// c code defines a static constant
static const int i=3;
%}
// declare the constant so that it shows up in the python module
static const int i;
%pythoncode %{
# try to use the constant in some python code
lookup={'i':i,}
%}以下是错误:
[dave]$ python -c "import test"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "test.py", line 70, in <module>
lookup={'i':i,}
NameError: name 'i' is not defined如果我在lookup块中注释掉了pythoncode字典,那么一切都很好:
[dave]$ python -c "import test; print test.i"
3所以,至少当我导入模块时,常量就会出现。
我如何在pythoncode块中“看到”C定义的常量?
大口2.0.4,python 2.7。
发布于 2015-07-10 19:36:36
另一个解决方法是将引用变量的时间推迟到模块完成加载之后,方法是使用一个函数:
%pythoncode %{
def lookup( key ){
mp={'i':i}
return mp[key]
%} https://stackoverflow.com/questions/31347788
复制相似问题