isNumber? check

Erik Max Francis max at alcyone.com
Tue Sep 30 15:04:20 EDT 2003


Jean-S?bastien Bolduc wrote:

> I used to use the latter approach suggested by Gerrit, but I recently
> found on the web an alternative, elegant approach that might work
> (sorry, I don't recall where I found it!):
> 
> hasattr(x, '__int__')
> 
> If the "__int__" method is defined for "x", it is a number. This will
> work for integer, long, float and complex types, as well as for custom
> classes that emulate numeric types.

This is an insidiously bad idea, in my opinion.  All having an __int__
method means is there is some _conversion_ from an instance to an int
type.  It does not at all mean the custom instance spends most of its
life behaving as an integer.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ We grow in time to trust the future for our answers.
\__/  Ruth Benedict




More information about the Python-list mailing list