I am using many variables with the same type in all functions of the same module:
def func1(double x):
cdef double a,b,c
a = x
b = x**2
c = x**3
return a+b+c
def func2(double x):
cdef double a,b,c
a = x+1
b = (x+1)**2
c = (x+1)**3
return a+b+c
My question is, will it be the same if I did as shown below? with the variable declaration placed outside the functions? (The real case is different and has more than 2 functions)
cdef double a,b,c
def func1(double x):
a = x+2
b = (x+2)**2
c = (x+2)**3
return a+b+c
def func2(double x):
a = x+2
b = (x+2)**2
c = (x+2)**3
return a+b+c
In principle, cython handles global variables as python does, no matter whether it's a C or Python type. Have a look at this part of the FAQ.
So your (second) example would not work, you'd have to use global variable
at the beginning of your function, like this:
def func2(double x):
global a, b, c
a = x + 2
b = (x + 2) ** 2
c = (x + 2) ** 3
return a + b + c
However, at this point I would like to question, whether you really need to do this. Quite generally, there are good arguments, why global variables are bad. So you seriously might want to reconsider.
I assume, that your three doubles are just a toy example, so I'm not sure what your actual use case is. Judging from your (first) example, reusing code could be done instead by extending the function by another parameter, like so:
def func(double x, double y=0):
cdef double a, b, c
a = x + y
b = (x + y) ** 2
c = (x + y) ** 3
return a + b + c
This would at least cover your examples func1
and func2
here by using y = 0
and y = 1
respectively.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments