[Tutor] __iter__ loops, partitioning list among children

Kent Johnson kent37 at tds.net
Tue Aug 26 20:22:30 CEST 2008


On Tue, Aug 26, 2008 at 1:24 PM, Eric Abrahamsen
<eric at ericabrahamsen.net> wrote:
> On Aug 26, 2008, at 7:20 PM, Kent Johnson wrote:

>> If all you want to do with the nested Month, etc is to iterate the
>> events in them, you could probably use a shared iterator. It would
>> have to be able to push-back items so that when you hit the
>> out-of-range item you could push it back on the iterator.
>
> Is a 'shared iterator' something special, or do you mean all the instances
> would draw their events from a single iterator? I'm not sure what this would
> look like.

It's nothing special, I just mean that all instances would share an
iterator. You would pass the iterator to the iteration function.

> Just for the sake of argument, here's the principle I'm working from:
>
> #####
>>>> lst = range(10)
>>>> iterlst = iter(lst)
>>>> iterlst.next()
> 0
>>>> for x in iterlst:
> ...   if x < 5:
> ...     print x
> ...   else:
> ...     break
> ...
> 1
> 2
> 3
> 4
>>>> for x in iterlst:
> ...   print x
> ...
> 6
> 7
> 8
> 9
> #####
>
> So that's why I'm creating the iterator outside of the while loop in the
> original code, and then using a repeated for loop with a break to step
> through all the events only once. Of course, the fact that 5 isn't in there
> probably points to the source of my problems! The solution might be
> assigning iterlist.next() to a variable, and advancing the variable.

The problem is that the first loop consumes the 5 from the iterator
but doesn't actually process it. That is why you need an iterator with
push-back - so you can put the 5 "back in" the iterator and get it out
again in the next loop.
http://code.activestate.com/recipes/502304/
Though working with indices directly might be simpler.

Kent


More information about the Tutor mailing list