[C++-sig] What if a Python override returns several values?
David Abrahams
dave at boost-consulting.com
Tue Sep 12 03:35:20 CEST 2006
Alex Mohr <amohr at pixar.com> writes:
>> But if I have another virtual member function "void g(int& a, int &b)"
>> which returns two integers, then I'd like the Python version to take no
>> arguments and return a tuple (a,b). But how does the wrapper look like
>> in this case? I would have expected something like:
>>
>> void g(int& a, int& b)
>> {
>> boost::python::tuple tup = this->get_override("g")();
>>
>> a = ...extract an int from tup[0]...
>> b = ...extract an int from tup[1]...
>> }
>>
>> But when I run this code I get the following error at runtime:
>>
>> TypeError: No registered converter was able to extract a C++ reference
>> to type boost::python::tuple from this Python object of type tuple
>
> Not sure exactly what's going on here, but you might be able to find out
> by looking at override.hpp. Look at method_result -- that's what you're
> getting back from get_override.
>
> Off the top of my head, I think this would probably work for you:
>
> tuple tup = call<tuple>(this->get_override("g").ptr());
> a = ...extract an int from tup[0]
> b = ...extract an int from tup[1]
I don't know why people always use ugly low-level interfaces when they
can use beautiful high-level ones:
python::tuple tup = this->get_override("g")();
a = extract<t1>(tup[0]);
.
.
.
There's almost never a reason to use call<> anymore! I should
deprecate it.
I'm not trying to be critical, but this really concerns me. I see
this sort of thing all the time. If you could help me understand
why you were moved to suggest doing it the hard way, I would
appreciate it.
> It's certainly not as nice as what you wrote,
But it could be. :)
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list