PyFloat_Check - help!

Adrian Eyre a.eyre at optichrome.com
Thu Oct 21 12:23:55 EDT 1999


> [code snipped]
>
> I would prefer the above code to translate Integer objects, where this
> makes sense. i.e. the rate above could be either 1 or 10 to represent 1%
> or 10%, but I have to explicitly put in a 1.0 or 10.0 to make it a
> float.

Quite simple. Just switch the type:

double rate;

switch (PyObject_Type(poRate))
{
case PyFloat_Type:
	rate = PyFloat_AsDouble(poRate);
	break;

case PyInt_Type:
	rate = (double) PyInt_AsLong(poRate);
	break;

case PyLong_Type:
	rate = PyLong_AsDouble(poRate);
	break;

default:
	PyErr_SetString(PyExc_TypeError, "Rate must be numerical");
	return NULL;
}

This should allow: 1, 10, 1.0, 10.0, and even 1L and 10L

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com 
--------------------------------------------





More information about the Python-list mailing list