[Numpy-discussion] Re: [Numpy-user] possible error with isarrtype

Travis Oliphant oliphant.travis at ieee.org
Tue Jan 31 13:23:03 EST 2006


O'Keefe, Michael wrote:

>I was just migrating some working scipy/numeric code I'd been using to the newer numpy (0.9.4), python (2.4.2), and scipy (0.4.4).
>
>When I ran my unit-tests, one line that gave me problems was:
>
>import scipy as sp
>...
>assert( type(deltaTime_sec)==float )
>
>This line of code was there to inform me if I was accidentally passing in an array type instead of a scalar. This no longer worked in the new environment. The type of deltaTime_sec came out as float64_arrtype which did not equal type float causing the assertion to trip which I believe to be caused by the following:
>  
>
Ah...  This is something that needs to be added to the FAQ, because it 
is common.  Basically, you should almost never test for type equality 
like this since with >Python2.2 the Python float can be inherited from 
(that's what a float64scalar does --- used to be called float64_arrtype).

So, instead you should use

assert (isinstance(deltaTime_sec, float))  which would still work with 
the new float scalar objects.

The big advantage of the new scalar types and objects is that there is a 
1-1 relationship between the scalar types and the basic array data-types 
and the new scalars have all the methods and attributes of arrays.

So, in fact you could write:

assert(deltaTime_sec.size == 1)

and this would work whether deltaTime is a scalar or an array with only 
one element.

-Travis








More information about the NumPy-Discussion mailing list