[Numpy-discussion] help! type 'float64scalar' is not type 'float'
oliphant.travis at ieee.org
Thu Aug 3 00:02:39 CDT 2006
Sebastian Haase wrote:
> 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
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
More information about the Numpy-discussion