check if object is number

Steven Bethard steven.bethard at gmail.com
Fri Feb 11 17:44:45 EST 2005


Fredrik Lundh wrote:
> Steven Bethard wrote:
>> Is there a good way to determine if an object is a numeric type?
> 
> assert operator.isNumberType(i)

Interesting, thanks!  If I read the source right, PyNumber_Check (which 
operator.isNumberType is an alias for) basically just returns True if 
the object's type has a __int__ or __float__ method defined:

int
PyNumber_Check(PyObject *o)
{
	return o && o->ob_type->tp_as_number &&
	       (o->ob_type->tp_as_number->nb_int ||
		o->ob_type->tp_as_number->nb_float);
}

Did I read that right?

Steve



More information about the Python-list mailing list