[Numpy-discussion] Python 3 and isinstance(np.int64(42), int)
Jens Jørgen Mortensen
jensj at fysik.dtu.dk
Thu Jun 18 01:53:31 EDT 2015
Hi!
I just finished porting a large code-base to Python 3 (making it work on
2.6, 2.7 and 3.4). It wasn't that difficult, but one thing gave me a
hard time and it was this:
Python 2.7.9 (default, Apr 2 2015, 15:33:21)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = np.zeros(7, int)
>>> n = a[3]
>>> type(n)
<type 'numpy.int64'>
>>> isinstance(n, int)
True
With Python 3.4 you get False. I think I understand why (np.int64 is no
longer a subclass of int). So, I did this instead:
import numbers
isinstance(n, numbers.Integral)
which works fine (with numpy-1.9). Is this the "correct" way or is
there a better way to do it? I would imagine that a lot of code will
break because of this - so it would be nice if isinstance(n, int) could
be made to work the same way in 2 and 3, but I don't know if this is
possible (or desirable).
Jens Jørgen
More information about the NumPy-Discussion
mailing list