views:

42

answers:

1

I'm using boost::python::extract<> to convert the items in a boost::python::list to floats. My problem is with int's in python - extract<float> seems to regard int->float as a valid conversion, however I only want true float objects. Is there a way to force extract<> to be more conservative?

extract<float> value(o);
if (value.check()) {
  // This is true both for floats and ints
  a = value();
}
+1  A: 

I'm pretty sure that you can't tell extract<float> not to convert intergers to floats.

What you could do is to query the wrapped PyObject:

const PyObject* pyo = o.ptr();
if (PyFloat_Check(pyo))
{
    // True only for floats.
    a = extract<float>(o);
}
Arlaharen
Great, that will do exactly what I need. Thanks!
Magnus W