[Cython] how to initialize my c array in a quick way?

Stefan Behnel stefan_ml at behnel.de
Mon Nov 10 09:07:34 CET 2008


Greg Ewing wrote:
> jay wrote:
>> I want declare an array with a  shape at the beginning of program,then
>> in my function i will assign value to it by calculation.
>> Is there a good way to do it?
>
> Ideally, what you *should* be able to do is
>
>    a[:] = 1, 3, 28, 5, 3
>
> but unless Cython has grown a feature I don't know about,

As I said, this works already:

    cdef int* a = [1, 3, 28, 5, 3]

However, a later assignment will not work. Your example above is
absolutely the way to go, and it would even work in the case where a is
declared as a plain pointer, not just a fixed size array.

Stefan



More information about the Cython-dev mailing list