I have a main python file (main.py) which declares a variable W as follows:
main.py imports a cython file 'my_lib.pyx" consisting of multiple cdef or cpdef functions that use or modify this W. Note that W may also be used or modified in main.py.
The question is how do we declare W in cython functions of ‘my_lib.pyx’ so that they regard W as a global function. It seems that declaring W as global either within these functions or outside is not honored and the tool complains that W is not known.
I don’t know about Cython specifically, but if it’s like Python, then “global” means global to the module. The file that you run directly (not through importing it) is given the name __main__. If you want to access it from another module, you would write:
Then, like any other module you import, you’d be able to refer to names in that file with (for example):
I don’t know if there’s any way that’s specific to Cython for doing this.