从暴露给Python的C ++函数返回int或字符串 [英] Return int or string from C++ function exposed to Python

查看:103
本文介绍了从暴露给Python的C ++函数返回int或字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Boost Python,暴露给python的C ++函数是否可能根据传入的单个参数的 value 返回整数或字符串(或其他类型)?

所以在Python中,我想这样做:

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 ++伪代码(显然,它不能像这样工作,但是您知道了)

???? 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);
}

如果有什么不同,我将在Python 3.2中使用Boost 1.48.

解决方案

让C ++函数返回 #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); }

及其用法:

 >>> 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'>
 

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.

解决方案

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'>

这篇关于从暴露给Python的C ++函数返回int或字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆