On Fri, Jun 19, 2015 at 4:15 PM, Chris Barker <chris.barker@noaa.gov> wrote:
On Wed, Jun 17, 2015 at 11:13 PM, Nathaniel Smith <njs@pobox.com> wrote:
 there's some
argument that in Python, doing explicit type checks like this is
usually a sign that one is doing something awkward,

I tend to agree with that.

On the other hand, numpy itself is kind-of sort-of statically typed. But in that case, if you need to know the type of an array -- check the array's dtype.


 >>> a = np.zeros(7, int)
 >>> n = a[3]
 >>> type(n)
<type 'numpy.int64'>

I Never liked declaring numpy arrays with the python types like "int" or "float" -- in numpy you usually care more about the type, so should simple use "int64" if you want a 64 bit int. And "float64" if you want a 64 bit float. Granted, pyton floats have always been float64 (on all platfroms??), and python ints used to a be a reasonable int type, but now that python ints are bigInts in py3, it really makes sense to be clear.

And now that I think about it, in py2, int is 32 bit on win64 and 64 bit on *nix64 -- so you're really better off being explicit with your numpy arrays.

being late checking some examples

>>> a = np.zeros(7, int)
>>> a.dtype
>>> np.__version__
>>> type(a[3])
<class 'numpy.int32'>

>>> a = np.zeros(7, int)
>>> a = np.array([888888888888888888])
>>> a
array([888888888888888888], dtype=int64)

>>> a = np.array([888888888888888888888888888888888])
>>> a
array([888888888888888888888888888888888], dtype=object)

>>> a = np.array([888888888888888888888888888888888], dtype=int)
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    a = np.array([888888888888888888888888888888888], dtype=int)
OverflowError: Python int too large to convert to C long

Looks like we need to be a bit more careful now.

Python 3.4.3




Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception


NumPy-Discussion mailing list