Re: Has Next in Python Iterators

2010-10-25 Thread Ian
On Oct 25, 4:33 am, Kelson Zawack wrote: > The example I have in mind is list like [2,2,2,2,2,2,1,3,3,3,3] where > you want to loop until you see not a 2 and then you want to loop until > you see not a 3.  In this situation you cannot use a for loop as > follows: > > foo_list_iter = iter([2,2,2,2,

Re: Has Next in Python Iterators

2010-10-25 Thread Paul Rudin
Kelson Zawack writes: > The example I have in mind is list like [2,2,2,2,2,2,1,3,3,3,3] where > you want to loop until you see not a 2 and then you want to loop until > you see not a 3. "loop until you see not a 2" - you mean yield 2s as long as there are 2s to be consumed? "loop until you see

Re: Has Next in Python Iterators

2010-10-25 Thread Hrvoje Niksic
Kelson Zawack writes: > Iterators however are a different beast, they are returned by the > thing they are iterating over and thus any special cases can be > covered by writing a specific implementation for the iterable in > question. This sort of functionality is possible to implement, > becaus

Re: Has Next in Python Iterators

2010-10-25 Thread Jussi Piitulainen
Kelson Zawack writes: > The example I have in mind is list like [2,2,2,2,2,2,1,3,3,3,3] > where you want to loop until you see not a 2 and then you want to > loop until you see not a 3. In this situation you cannot use a for > loop as follows: ... > because it will eat the 1 and not allow the sec

Re: Has Next in Python Iterators

2010-10-25 Thread Stefan Behnel
Kelson Zawack, 25.10.2010 12:33: The example I have in mind is list like [2,2,2,2,2,2,1,3,3,3,3] where you want to loop until you see not a 2 and then you want to loop until you see not a 3. In this situation you cannot use a for loop as follows: foo_list_iter = iter([2,2,2,2,2,2,1,3,3,3,3]) fo

Re: Has Next in Python Iterators

2010-10-25 Thread Kelson Zawack
The example I have in mind is list like [2,2,2,2,2,2,1,3,3,3,3] where you want to loop until you see not a 2 and then you want to loop until you see not a 3. In this situation you cannot use a for loop as follows: foo_list_iter = iter([2,2,2,2,2,2,1,3,3,3,3]) for foo_item in foo_list_iter: if

Re: Has Next in Python Iterators

2010-10-23 Thread Alexander Gattin
Hello, On Thu, Oct 21, 2010 at 12:26:50PM +, Steven D'Aprano wrote: > I know what you're thinking: "it's easy to cache > the next result, and return it on the next > call". But iterators can also be dependent on > the time that they are called, like in this > example: > > def evening_time():

Re: Has Next in Python Iterators

2010-10-21 Thread Paul Rudin
Kelson Zawack writes: > Since an iterator having an end is not actually an exceptional case... There's no requirement on iterators to be finite, so in a sense it is. In general it may be impractical to know whether an iterator has reached the end without calling next(). -- http://mail.python

Re: Has Next in Python Iterators

2010-10-21 Thread Steven D'Aprano
On Thu, 21 Oct 2010 19:08:00 +0800, Kelson Zawack wrote: > I have been programing in python for a while now and by in large love > it. One thing I don't love though is that as far as I know iterators > have no has_next type functionality. As a result if I want to iterate > until an element that

Re: Has Next in Python Iterators

2010-10-21 Thread Steven D'Aprano
On Thu, 21 Oct 2010 09:36:41 -0200, Felipe Bastos Nunes wrote: > Looking in the documentation, only the StopIteration raises. I'd like a > hasNext() too. I'll see if it is easy to implement, Iterators can be unpredictable. In general, you can't tell whether an iterator is finished or not until

Re: Has Next in Python Iterators

2010-10-21 Thread Ben Finney
Kelson Zawack writes: > […] if I want to iterate until an element that might or might not be > present is found I either wrap the while loop in a try block or break > out of a for loop. I'm not sure what exception you would catch, but that could be a good solution. The general solution would b

Re: Has Next in Python Iterators

2010-10-21 Thread Felipe Bastos Nunes
Looking in the documentation, only the StopIteration raises. I'd like a hasNext() too. I'll see if it is easy to implement, but maybe it'sn ot yet there coz for does the work greatly. 2010/10/21, Kelson Zawack : > I have been programing in python for a while now and by in large love > it. One thi