Search code examples
pythonc++python-c-apipython-c-extension

Python: C++ extension returning multiple values


I am writing a C++ extension for python script and want to return multiple values like what we can do in python function.

Simple Example in python:

def test():
    return 0,0

tuple seems to be the closest answer

#include <tuple>

std::tuple<int,int> test(void){
return std::make_tuple(0,0);
}

But when I compile it, it complains that

TypeError: No to_python (by-value) converter found for C++ type: std::tuple<int, int>

Anyone knows if I could return multiple values using C++?

EDIT:

This is my setup.py file.

#!/usr/bin/env python

from distutils.core import setup
from distutils.extension import Extension

setup(name="PackageName",
    ext_modules=[
        Extension("foo", ["foo.cpp"],
        libraries = ["boost_python"],
        extra_compile_args = ["-std=c++11"]
        )
    ])

Solution

  • It seems you're using boost-python. Then should use boost::python::tuple, not std::tuple. See the examples on this page.