Another C API Question

Robert Kern robert.kern at
Fri Jul 27 03:16:27 CEST 2007

beginner wrote:
> Hi,
> I run into another C API question. What is the simplest way to convert
> an PyObject into a double?
> For example, I have
>  PyObject *obj;
> I know obj is a number, but I do not know the exact type. How can I
> convert it to double without writing a giant switch() that exhausts
> every single type of number?

Convert it to a Python float using PyNumber_Float(), then use PyFloat_AsDouble()
to get the C double value from it.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the Python-list mailing list