[Numpy-discussion] Cython/NumPy syntax
Dag Sverre Seljebotn
Wed Aug 6 09:45:39 CDT 2008
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 <email@example.com>:
>>>>> - 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 = \
to construct a new array and get buffer access to it right away).
Also, the argument list on the type is not defined by the ndarray
constructor but corresponds to your buffer PEP, and I think using ()
obscures this fact.
For NumPy this is not such a big problem as the argument lists will be
rather similar, but for other libraries than NumPy supporting the buffer
PEP the argument list may diverge more:
cdef MyJpegImage(dtype=unsigned char, ndim=2) jpg = \
(and even worse if the keywords are not mandatory).
Another example: One can currently do (dropping keywords)
cdef object[double, 4, "strided"] buf = ...
to get generic 4D strided buffer access that doesn't make assumptions
about the class, but the Python object() takes no parameters...
(BTW, there is a mechanism so that simpler buffer exporters like
MyJpegImage can declare default buffer options, so one is unlikely to
see the above in practice -- if the type always has the same dtype and
ndim, it is enough to do
cdef MyJpegImage jpg = ...
For ndarray I use the same mechanism to let mode="strided" by default.
But I should start writing documentation rather than making this email
longer :-) )
More information about the Numpy-discussion