[Numpy-discussion] Cython numerical syntax revisited

Dag Sverre Seljebotn dagss@student.matnat.uio...
Thu Mar 5 04:41:58 CST 2009

Sturla Molden wrote:
>> Introducing this syntax would actually mean less time to focus on "real 
>> usability issues" like that. OTOH, if the syntax I propose is superior, 
>> it's better to introduce it early in a long-term perspective.
> There is not much difference between
>     cdef int[:,:] array
> and
>     cdef numpy.ndarray[int, dim=2] array
> except that the latter is a Python object. The only minor issue with 
> that is the GIL. On the other hand, the former is not a Python object, 
> which means it is not garbage collected.
As with all syntax, the difference is mostly psychological. The former 
means "now I need fast access and will want to hit the metal, and will 
no longer look on my array through a NumPy object but through a buffer 
view", whether the latter is "let Cython can optimize some of the NumPy 

About garbage collection, int[:,:] would always be a buffer view unto an 
underlying object which *would* be garbage collected. I.e. it is *not* 
stack-allocated memory; so when you do

cdef np.int_t[:,:] arr = np.zero((10,10), np.int)

then the memory of the array is garbage collected insofar the result of 
np.zero is. "arr" simply adds a reference to the underlying object (and 
slices add another reference, and so on).

Support for GIL-less programming is on the wanted-list anyway for both 
syntaxes though; Cython can now when one does something illegal and only 
let through certain uses of the variable, so both syntaxes works for this.

Dag Sverre

More information about the Numpy-discussion mailing list