Recieve global variable (Cython)

2024/9/21 5:44:28

I am using Cython in jupyter notebook.

As I know, Cython compiles def functions.

But when I want to call function with global variable it doesn't see it.

Are there any method to call function with variable?

one1 = 1
%%cython
cimport numpy as np
cdef nump(number1):return number1 + 1
nump(one1)

****This is sample code, to show moderators

Answer

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
https://en.xdnf.cn/q/119236.html

Related Q&A

Counting elements in specified column of a .csv file

I am programming in Python I want to count how many times each word appears in a column. Coulmn 4 of my .csv file contains cca. 7 different words and need to know how many times each one appears. Eg. t…

Why does genexp(generator expression) is called genexp? not iterexp?

A generator is a special kind of iterator, and it has some methods that an normal iterator doesnt have such as send(), close()... etc. One can get a generator by using a genexp like below:g=(i for i in…

why does no picture show

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg if __name__ == "__main__":fig1 = ...print("start plotting")canvas = FigureCanvasQTAgg(fig1)canvas.draw()canvas.show(…

How Normalize Data Mining Min Max from Mysql in Python

This is example of my data in mysql, I use lib flashext.mysql and python 3RT NK NB SU SK P TNI IK IB TARGET 84876 902 1192 2098 3623 169 39 133 1063 94095 79194 …

complex json file to csv in python

I need to convert a complex json file to csv using python, I tried a lot of codes without success, I came here for help,I updated the question, the JSON file is about a million,I need to convert them t…

python pygame - how to create a drag and drop with multiple images?

So Ive been trying to create a jigsaw puzzle using pygame in python.The only problem is that Im having trouble creating the board with multiple images that i can drag along the screen (no need to conne…

Efficiently append an element to each of the lists in a large numpy array

I have a really large numpy of array of lists, and I want to append an element to each of the arrays. I want to avoid using a loop for the sake of performance. The following syntax is not working. a=np…

How to traverse a high-order range in Python? [duplicate]

This question already has answers here:Equivalent Nested Loop Structure with Itertools(2 answers)Closed 4 years ago.In python, we can use range(x) to traverse from 0 to x-1. But what if I want to trave…

How to send eth_requestAccounts to Metamask in PyScript?

I am trying to get address from installed MetaMask on the browser. We used to do this in JS as follow:const T1 = async () => {let Address = await window.ethereum.request({method: "eth_requestAc…

Extract strings that start with ${ and end with }

Im trying to extract the strings from a file that start with ${ and ends with } using Python. I am using the code below to do so, but I dont get the expected result.My input file looks like this:Click …