小编pyt*_*ter的帖子

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)

但是我得到了 …

python variables global cython

4
推荐指数
1
解决办法
6042
查看次数

标签 统计

cython ×1

global ×1

python ×1

variables ×1