Alternative iterator syntax

Eduard Hiti KatMouse at gmx.de
Wed Feb 21 21:18:43 CET 2001


| Predefined Usage of Iterators:
|
| 1. The looping structure:
|
|   Consider the structure
|
|     for a, b, c, ... in A:
|         do something
|     else:
|         do otherthing
|
|   If A defines __next__, the above is interpreted as the equivalence of
|   (__got is an invisible variable):
|
|     __got = 0
|     while 1:
|         try:
|             a, b, c, ... = A.__next__()
|             __got = 1
|             do something
|         except IndexError:
|             break
|     if not __got:
|         do otherthing
|

I think there is an error in the code above. Shouldn't it rather go like:

     __exhausted = 0
     while 1:
         try:
             a, b, c, ... = A.__next__()
             do something
         except IndexError:
             __exhausted = 1
             break
     if __exhausted:
         do otherthing


The 'do something' block can break to avoid executing 'do otherthing'.






More information about the Python-list mailing list