You can use the __length_hint__() method stop the iter's loop. Like is:
while(extract<int>(my_iterable_obj.attr("__length_hint__")()) > 0):
{
    ...
}

This way can useful at list, tuple and as other Python's containter.

2006/11/11, Augusto Callejas <acallejas@appliedminds.com>:
hi-

i'm writing a C++ class that i want to expose as a python class.
one method in my class has an argument that takes in a python
object that "implements" the iterator interface (that is the
python object has defined "__iter__" and "__next__" and can
be used in a "for x in y" loop).

how do i go about iterating over that python object in my C++ code?

for example:

void my_class::method(object my_iterable_obj)
{
  // iterate over "my_iterable_obj"
}

one method i tried was calling the "__next__" method explicity:

  my_iterable_obj.attr("__next__")()

however, "__next__" raises a StopIteration exception when there
are no more objects in the iterator.  i could not find any examples
in the boost python tutorials for handling python exceptions from c++.

is there another way i can go about this?

thanks,
augusto.


_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users



--
»¶Ó­·ÃÎÊ£º
http://blog.csdn.net/ccat

ÁõöÎ
March.Liu