2

Using Boost Python, is it possible for a C++ function exposed to python to return either an integer or a string (or other type) depending on the value of the single argument passed in?

So in Python I want to do this:

from my_module import get_property_value     

# get an integer property value
i = get_property_value("some_int_property")

# get a string 
s = get_property_value("some_string_property")

C++ pseudo code (obviously won't it work like this, but you get the idea)

???? getPropertyValue(const char* propertyName)
{
  Property *p = getProperty(propertyName);
  switch(p->type)
  {
    case INTEGER: return p->as_int();
    case STRING: return p->as_string();
    ...
  }
}


BOOST_PYTHON_MODULE(my_module)
{
  boost::python::def("get_property_value", &getPropertyValue);
}

If it makes any difference, I'm using Boost 1.48 with Python 3.2.

4 Answers 4

3

Have the C++ function return a boost::python::object. The object constructor will attempt to converts its argument to the appropriate python type and manages a reference to it. For example, boost::python::object(42) will return a Python object with a Python type of int.


Here is a basic example:

#include <boost/python.hpp>

/// @brief Get value, returning a python object based on the provided type
///        string.
boost::python::object get_value(const std::string& type)
{
  using boost::python::object;
  if      (type == "string") { return object("string 42"); }
  else if (type == "int")    { return object(42);          }
  return object(); // None
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::def("get_value", &get_value);
}

and its usage:

>>> import example
>>> x = example.get_value("string")
>>> x
'string 42'
>>> type(x)
<type 'str'>
>>> x = example.get_value("int")
>>> x
42
>>> type(x)
<type 'int'>
>>> x = example.get_value("")
>>> x
>>> type(x)
<type 'NoneType'>
Sign up to request clarification or add additional context in comments.

1 Comment

tks, boost::python::object saves my day
2

I would suggest you make the C++ function return an object. It will have to do the appropriate conversions internally.

Comments

0

How's this?

string getPropertyValue(cont char* propertyName)
{
    // do some stuff
    if (someCondition)
    {
        return "^" + someInteger;
    }
    else
    {
        return someString; // if someString starts with the character '^', make
                           // it start with "^^" instead.
    }
}

Then in Python, if the return value doesn't start with "^", use it as-is as a string. Otherwise if it starts with "^^", trim one of them off and then use it as a string. Otherwise trim the one "^" off the beginning and use it as an int.

Comments

-1

I don't think you can do this in c (or c++). Worse, you're trying to use a function pointer - which means you can't even overload.

What you can do is build a Property Class with methods for property->as_int() and property->as_string();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.