[C++-sig] Re: Problem with boost::python::iterator

David Abrahams dave at boost-consulting.com
Tue Dec 9 05:11:11 CET 2003


Pierre Barbier de Reuille <pierre.barbier at cirad.fr> writes:

> I use boost::python::iterator to export a python iterator from STL
> containers. More precisely, I use :
>
>   .def("__iter__", boost::python::iterator<Container>())
>
> where Container is a STL container ( vector<foo> for example ). Let's
> say that foo is a class that contains an integer 'a'. My problem is with
> this following test:
>
>>>> l = vector_foo()
>>>> l.append(foo(3))
>>>> l.append(foo(2))
>>>> l.append(foo(10))
>>>> print l
> vector_foo{ foo(3), foo(2), foo(10) }
>>>> for i in l:
> ...   i.a = i.a + 3
> ...
>>>> print l
> vector_foo{ foo(3), foo(2), foo(10) }
>
> I don't understand !!! It used to work

When?  That's surprising.  Does the documentation for the default type
of NextPolicies at
http://www.boost.org/libs/python/doc/v2/iterator.html#iterator-spec
explain why?

> , but it does not anymore ! If you
> replace the variable (defined as readwrite) by a function call modifying
> the object it does not modify the vector !
>
> This is a very big problem for me !!! What can I do ?

   .def(
       "__iter__"
     , boost::python::iterator<
            Container
          , return_internal_reference<>
       >()
   )

??

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list