[Numpy-discussion] isbuiltin - failure of understanding

Robert Kern robert.kern at gmail.com
Tue Feb 24 16:10:11 EST 2009


On Tue, Feb 24, 2009 at 15:04, Matthew Brett <matthew.brett at gmail.com> wrote:
> Hi,
>
> I was just trying to write a docstring for np.dtype.isbuiltin, when I
> realized I didn't understand it.
>
> As far as I can see, isbuitin should return:
>
> 0 for structured array dtypes
> 1 for types compiled into numpy
> 2 for extension types using the numpy C-API type extension machinery.
>
> Here's the C code:
>
> static PyObject *
> arraydescr_isbuiltin_get(PyArray_Descr *self)
> {
>    long val;
>    val = 0;
>    if (self->fields == Py_None) val = 1;
>    if (PyTypeNum_ISUSERDEF(self->type_num)) val = 2;
>    return PyInt_FromLong(val);
> }
>
> But, why is this?
>
> In [2]: dt = np.dtype('S1')
>
> In [3]: dt
> Out[3]: dtype('|S1')
>
> In [4]: dt.isbuiltin
> Out[4]: 0
>
> In [5]: print dt.fields
> None
>
> In [6]: print dt.fields == None
> True
>
> Same for np.dtype('U1')

The variable-length string dtypes are not builtin. The user-defined
lengths make them user-defined dtypes.

If you want a real kick in the pants, try playing with dtype(str). And
then try eval'ing its repr.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list