pyt*_*ter 4 python variables global 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
Run Code Online (Sandbox Code Playgroud)
funktionen_a.c
#include "funktionen_a.h"
void setValue(int value_to_set){
value = value_to_set;
}
int readValue(){
return value;
}
Run Code Online (Sandbox Code Playgroud)
funktionen_a.h
#include <Python.h>
#include <stdio.h>
void setValue(int value_to_set);
int readValue();
int value;
Run Code Online (Sandbox Code Playgroud)
有了这个功能,我控制了整个事情:
控件.py
import pyfunktionen_a
pyfunktionen_a.pysetValue(8)
pyfunktionen_a.pyreadValue()
pyfunktionen_a.manipulateValue(5)
pyfunktionen_a.pyreadValue()
Run Code Online (Sandbox Code Playgroud)
我期望什么结果:
>> 8
>> 5
Run Code Online (Sandbox Code Playgroud)
但是我得到了什么结果:
>> 8
>> 8
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用:
def manipulateValue(value_to_set):
global value
value = value_to_set
Run Code Online (Sandbox Code Playgroud)
否则 value 将是此函数中的局部变量。
此链接可能有用:https : //github.com/cython/cython/wiki/FAQ#id34