[Numpy-discussion] type checking, what's recommended?

Olivier Delalleau shish at keba.be
Wed Dec 7 21:01:06 EST 2011


We have indeed been using "type(a) is np.ndarray" in Theano to check that.
If there's a better way, I'm interested to know as well :)

-=- Olivier

2011/12/7 <josef.pktd at gmail.com>

> If I want to know whether something that might be an array is really a
> plain ndarray and not a subclass, is using `type` the safest bet?
>
> All the other forms don't discriminate against subclasses.
>
> >>> type(np.ma.zeros(3)) is np.ndarray
> False
> >>> type(np.zeros(3)) is np.ndarray
> True
>
> >>> isinstance(np.ma.zeros(3), np.ndarray)
> True
> >>> isinstance(np.zeros(3), np.ndarray)
> True
>
> >>> issubclass(np.ma.zeros(3).__class__, np.ndarray)
> True
> >>> issubclass(np.zeros(3).__class__, np.ndarray)
> True
>
> >>> isinstance(np.matrix(np.zeros(3)), np.ndarray)
> True
> >>> type(np.matrix(np.zeros(3))) is np.ndarray
> False
>
> Thanks,
>
> Josef
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111207/feae68f7/attachment.html>


More information about the NumPy-Discussion mailing list