Boost logo

Boost Users :

From: ÁõöÎ (march.liu_at_[hidden])
Date: 2006-11-12 07:53:05


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_at_[hidden]>:
>
> 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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
»¶Ó­·ÃÎÊ£º
http://blog.csdn.net/ccat
ÁõöÎ
March.Liu


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net