Cython 直接访问全局变量
我想知道怎么在Cython中访问一个全局变量,而不使用访问函数。
我试了下面这个例子:
pyfunktionen_a.pyx
import numpy as np
cdef extern from "funktionen_a.h":
cdef void setValue(int value_to_set)
cdef int readValue()
cdef int value
def pysetValue (_value):
setValue(_value)
def pyreadValue():
print readValue()
def manipulateValue(value_to_set):
value = value_to_set
funktionen_a.c
#include "funktionen_a.h"
void setValue(int value_to_set){
value = value_to_set;
}
int readValue(){
return value;
}
funktionen_a.h
#include <Python.h>
#include <stdio.h>
void setValue(int value_to_set);
int readValue();
int value;
然后我用这个函数来控制整个过程:
control.py
import pyfunktionen_a
pyfunktionen_a.pysetValue(8)
pyfunktionen_a.pyreadValue()
pyfunktionen_a.manipulateValue(5)
pyfunktionen_a.pyreadValue()
我期待的结果是:
>> 8
>> 5
但是我得到的结果是:
>> 8
>> 8
1 个回答
7
你可以试试这个:
def manipulateValue(value_to_set):
global value
value = value_to_set
否则,值将会成为这个函数里的一个局部变量。
这个链接可能对你有帮助: https://github.com/cython/cython/wiki/FAQ#id34