Chris Colbert wrote:
> Just recently got into working with cython for speeding up some calculation
> with numpy.
>
> this question, however, has nothing to do with numpy.
>
> The following code is valid python:
>
> var = 0
>
> def count(n):
> for i in range(n):
> var += i
>
> return var
Valid only in the sense that it passes the parser.
>>> count(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in count
UnboundLocalError: local variable 'var' referenced before assignment
> Translating to cython results in run-time errors:
>
> cdef double var = 0.0
>
> def count(int n):
> cdef int i
> for i in range(n):
> var += i
>
> return var
Just like in Python. Cython currently isn't smart enough to figure out at
compile time that the local variable var is never initialised in your
example. It therefore assigns None to it on initialisation to make sure
things don't break at runtime.
As in Python, you need to use the "global" statement to make the above work.
> The runtime error i get is 'can't assign nonetype to int'
Didn't try, but I doubt that that's the exact error message.
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev