Will a variable declared with cdef outside a function have the same type inside the function?

Saullo G. P. Castro

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
aepsil0n

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use a variable declared in a function outside that function

From Dev

How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

From Dev

What is the value of the static variable declared inside a function?

From Dev

Type of variable declared with decltype (having function as an expression)

From Dev

Different results of the same operation inside and outside function

From Dev

Calling a JavaScript variable inside a function from outside

From Dev

How to make a variable inside a function be available outside?

From Dev

static variable scope inside and outside the function

From Dev

Permanently changing an outside variable inside a function

From Dev

Unable to reference one particular variable declared outside a function

From Dev

When does a variable need to be declared outside a function in Javascript?

From Dev

Global variable declared outside function doesn't work

From Dev

What memory used for variable declared outside of a method or function

From Dev

array variable inside function affects the array variable outside the function in c

From Dev

array variable inside function affects the array variable outside the function in c

From Java

Will a function declared inside main() have external linkage or none linkage?

From Dev

Do you have to deallocate a const int if you declared it inside a function?

From Dev

return variable name from outside of function, as string inside python function

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

Bash array declared in a function is not available outside the function

From Dev

Javascript: While the variable is declared in global scope, it remains undefined inside the function

From Dev

Function works inside "$(function() {" but not outside?

From Dev

Why does a variable of a function declared outside its function definition doesn't throw an error?

From Dev

PHP undefined variable inside the same function

From Dev

Removing an action declared inside a function

From Dev

Should I declare a variable inside or outside the main function?

From Dev

Javascript: Why array variable assignment is behaving differently inside and outside this function?

From Dev

The right way of using a variable - inside or outside a function loop

Related Related

  1. 1

    Use a variable declared in a function outside that function

  2. 2

    How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

  3. 3

    What is the value of the static variable declared inside a function?

  4. 4

    Type of variable declared with decltype (having function as an expression)

  5. 5

    Different results of the same operation inside and outside function

  6. 6

    Calling a JavaScript variable inside a function from outside

  7. 7

    How to make a variable inside a function be available outside?

  8. 8

    static variable scope inside and outside the function

  9. 9

    Permanently changing an outside variable inside a function

  10. 10

    Unable to reference one particular variable declared outside a function

  11. 11

    When does a variable need to be declared outside a function in Javascript?

  12. 12

    Global variable declared outside function doesn't work

  13. 13

    What memory used for variable declared outside of a method or function

  14. 14

    array variable inside function affects the array variable outside the function in c

  15. 15

    array variable inside function affects the array variable outside the function in c

  16. 16

    Will a function declared inside main() have external linkage or none linkage?

  17. 17

    Do you have to deallocate a const int if you declared it inside a function?

  18. 18

    return variable name from outside of function, as string inside python function

  19. 19

    AngularJS: variable $scope inside a function appears undefined outside the function

  20. 20

    AngularJS: variable $scope inside a function appears undefined outside the function

  21. 21

    Bash array declared in a function is not available outside the function

  22. 22

    Javascript: While the variable is declared in global scope, it remains undefined inside the function

  23. 23

    Function works inside "$(function() {" but not outside?

  24. 24

    Why does a variable of a function declared outside its function definition doesn't throw an error?

  25. 25

    PHP undefined variable inside the same function

  26. 26

    Removing an action declared inside a function

  27. 27

    Should I declare a variable inside or outside the main function?

  28. 28

    Javascript: Why array variable assignment is behaving differently inside and outside this function?

  29. 29

    The right way of using a variable - inside or outside a function loop

HotTag

Archive