[C++-sig] Boost Python Tuple - Find length of tuple?

Stefan Seefeld seefeld at sympatico.ca
Mon Jan 4 15:08:39 CET 2010


On 01/04/2010 08:53 AM, Simon Pickles wrote:
> Hello,
>
> I have a simple question. How do I find out in c++ how many elements 
> are contained within a boost::python::tuple?
>
> I tried this:
>
>        using namespace boost::python;
>        tuple a = make_tuple("hello", 42);
>        object b = a.attr("length"); // exception
>        unsigned int c = extract<unsigned int>(b);
>        assert( c==2 );
>
> AttributeError: 'tuple' object has no attribute 'length'
>
> Am I missing something?

The attribute is spelled "__len__", not "length", and the pythonic way 
to call it is "len(a)". Thus:

tuple a = ...;
unsigned int c = len(a);

should work fine.

     Stefan

-- 

       ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list