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 ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion