[Numpy-discussion] int and long issues
Sebastian Berg
sebastian@sipsolutions....
Thu Jan 10 05:06:54 CST 2013
On Thu, 2013-01-10 at 11:32 +0100, Mads Ipsen wrote:
> Hi,
>
> I find this to be a little strange:
>
> x = numpy.arange(10)
> isinstance(x[0],int)
>
> gives True
>
> y = numpy.where(x < 5)[0]
> isinstance(y[0],int)
>
> gives False
>
> isinstance(y[0],long)
>
Check what type(x[0])/type(y[0]) prints, I expect these are very
different, because the default integer type and the integer type used
for indexing (addressing memory in general) are not necessarily the
same. And because of that, `y[0]` probably simply isn't compatible to
the datatype of a python integer for your hardware and OS (for example
for me, your code works). So on python 2 (python 3 abolishes int and
makes long the only integer, so this should work as expected there) you
have to just check both even in the python context, because you can
never really know (there may be some nice trick for that, but not sure).
And if you want to allow for rare 0d arrays as well (well they are very
rare admittingly)... it gets even a bit hairier.
Regards,
Sebastian
> gives True
>
> Specs: Python 2.7.2, numpy-1.6.1, Win7, 64 bit
>
> Best regards,
>
> Mads
> --
> +-----------------------------------------------------+
> | Mads Ipsen |
> +----------------------+------------------------------+
> | Gåsebæksvej 7, 4. tv | |
> | DK-2500 Valby | phone: +45-29716388 |
> | Denmark | email: mads.ipsen@gmail.com |
> +----------------------+------------------------------+
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list