[Solved] Recieve global variable (Cython)


In an Ipython session I can do:

In [2]: %load_ext Cython
In [3]: one = 1
In [4]: %%cython
   ...: def foo(num):
   ...:    return num + 1
   ...: 
In [5]: foo(one)
Out[5]: 2

That is I define a cython function, but call it from Python with the global variable.

If I define the function with cdef I need to invoke it with a def function. fooc is not visible from Python.

In [9]: %%cython
   ...: cdef fooc(num):
   ...:    return num + 2
   ...: def foo_call(num):
   ...:    return fooc(num)
   ...: 
In [10]: foo_call(one)
Out[10]: 3

If I attempt to use one from within the cython file (magic cell) I get an error, the equivalent of a Python NameError

In [8]: %%cython
   ...: cdef fooc(num):
   ...:    return num + 2
   ...: print(fooc(one))
   ...: 

Error compiling Cython file:
...
/home/paul/.cache/ipython/cython/....: undeclared name not builtin: one

The ipython session variable one is not visible from within the magic cell.


Working from @DavidW’s answer, this import works:

In [14]: %%cython
    ...: from __main__ import one
    ...: cdef fooc(num):
    ...:    return num + 2
    ...: print(fooc(one))
    ...: 
3

This fooc is not accessible from Python.

Note that the import uses the value of one at compile time.

In [22]: %%cython
    ...: from __main__ import one
    ...: cdef fooc(num):
    ...:    return num + 20
    ...: def fooc_call():
    ...:    print(fooc(one))
    ...: 
    ...: 
In [23]: fooc_call()
21
In [24]: one=343           # new value
In [25]: fooc_call()        # no change
21
In [26]: foo_call(one)     # uses the current value
Out[26]: 345

4

solved Recieve global variable (Cython)