[Numpy-discussion] Cython/NumPy syntax

Dag Sverre Seljebotn dagss@student.matnat.uio...
Wed Aug 6 10:04:39 CDT 2008

Dag Sverre Seljebotn wrote:
> Travis E. Oliphant wrote:
>> Gael Varoquaux wrote:
>>> On Wed, Aug 06, 2008 at 10:35:06AM +0200, Dag Sverre Seljebotn wrote:
>>>> Stéfan van der Walt wrote:
>>>>> 2008/8/6 Dag Sverre Seljebotn <dagss@student.matnat.uio.no>:
>>>>>> - Require an ndim keyword:
>>>>>> cdef numpy.ndarray[numpy.int64, ndim=2]
>> Just out of curiousity.  What is the problem with using parenthesis for 
>> this purpose?
>> cdef numpy.ndarray(dtype=numpy.int64, ndim=2)
> There's no technical problem, but we thought that it looked too much 
> like constructor syntax -- it looks like an ndarray is constructed. If 
> one is new to Cython this is what you will assume, at least the [] makes 
> you stop up and think more.
> (Which, for clarity, I should mention that it is not -- you'd do
> cdef np.ndarray(dtype=np.int64, ndim=1) buf = \
>    np.array([1,2,3], dtype=np.int64)
> to construct a new array and get buffer access to it right away).

I realize that I've given too little context for this discussion. This 
tends to get rather longwinded, but I'll provide it for whoever is 

What I am doing is supporting general syntax candy for the buffer PEP 
(and a backwards compatability layer for earlier Python versions) so that

cdef object[float, 2] buf = input

acquires a buffer and lets you use it using the indexing operator with 2 
native int indices, as well as letting other operations (also any 
indexing that doesn't have exactly 2 ints) fall through to the 
underlying object.

The most explicit syntax would be

cdef ndarray arr = input
cdef buffer[float, 2] buf = cython.getbuffer(arr)
arr += 4.3
buf[3,2] = 2

But that is very unfriendly to use. A step down in explicitness is

cdef buffer(ndarray, float, 2) arr = input
arr += 4.3 # falls through to ndarray type
arr[3,2] = 2 # uses buffer

But overall just adding something to the end of ndarray and make it 
completely transparent seemed most usable.

(Another option would be "cdef ndarray,buffer(float,2) arr = ...", i.e. 
arr "has two types".).

Dag Sverre

More information about the Numpy-discussion mailing list