[Numpy-discussion] help! type 'float64scalar' is not type 'float'

Sebastian Haase haase at msg.ucsf.edu
Thu Aug 3 00:16:59 CDT 2006


Travis Oliphant wrote:
> Sebastian Haase wrote:
>> Hi!
>> I just finished maybe a total of 5 hours tracking down a nasty bug.
>>
>> Finally I traced the problem down to a utility function:
>> "is_number" - it is simply implemented as
>> def is_number(val):
>>      return (type(val) in [type(0.0),type(0)])
>>
>> As I said - now I finally saw that I always got
>> False since the type of my number (0.025) is
>>   <type 'float64scalar'>
>> and that's neither <type 'float'> nor <type 'int'>
>>
>> OK - how should this have been done right ?
>>
>>   
> 
> Code that depends on specific types like this is going to be hard to 
> maintain in Python because many types could reasonably act like a 
> number.   I do see code like this pop up from time to time and it will 
> bite you more with NumPy (which has a whole slew of scalar types).
> 
> The scalar-types are in a hierarchy and so you could replace the code with
> 
> def is_number(val):
>      return isinstance(val, (int, float, numpy.number))
> 
> But, this will break with other "scalar-types" that it really should 
> work with.   It's best to look at what is calling is_number and think 
> about what it wants to do with the object and just try it and catch the 
> exception.
> 
> -Travis
> 

Thanks,
I just found
numpy.isscalar() and numpy.issctype()   ?
These sound like they would do what I need - what is the difference 
between the two ?
(I found that issctype worked OK while isscalar gave some exception in 
some cases !? )

- Sebastian






More information about the Numpy-discussion mailing list