bool (iterator)

Chirayu thephoenix235 at gmx.net
Mon Aug 5 01:39:33 EDT 2002


Hi people,

Is there a way for me to check if an iterator has "run out". I was hoping 
bool (iterator) would help but it always returns true.

example:

iterObj = iter (obj)
while 1:
    for i in iterObj:
       process i
       break based on some condition
    # continue until we've exhausted the iterator
    if bool (iterObj)==0: break


Below is a place in my project where i would have liked it. Read it if you 
have the time.

Here, I'm generating a dialog box dynamically from a tree structure.

case (1): without an iterator

def AddRecursive (parentSizer, obj):
    if obj.children: # obj.children = list of children
       leaves = []
       for i in obj.children:
          if i.children:
             leaves.append (i)
          else:
             if leaves:
                sizer = XXX # create a new sizer and add to parentSizer
                for j in leaves: AddRecursive (sizer, j)
                # add all the leaves to the new sizer
                leaves = []
             AddRecursive (parentSizer, i)
       if leaves:
          sizer = XXX # create a new sizer and add to parentSizer
          for j in leaves: AddRecursive (sizer, j)
          # add all the leaves to the new sizer
          leaves = []
    else:
       # handle the other case


Here, the lines in blue (if its plain text, then the code for "if leaves:" 
is repeated twice because I have to process the leaves either (1) when i 
dont find a leaf in the traversal, or (2) when the traversal is complete.

A better way would be

case (2): with an iterator

def AddRecursive (parentSizer, obj):
    leaves = []
    if obj.children: # obj.children = list of children
       iterChildren = iter (obj.children)
       while 1:
          for i in iterChildren:
             if i.children: leaves.append (i)
             else: break
          if leaves:
             sizer = XXX # create a new sizer and add to parentSizer
             for j in leaves: AddRecursive (sizer, j)
             # add all the leaves to the new sizer
             leaves = []
          AddRecursive (parentSizer, i)
          if not(bool(iterChildren)): break
    else:
       #handle the other case

the problem is that to terminate the while loop, i need to know if the 
iterator has "finished". bool(iterChildren) always returns true.

If anyone has a better solution, please do tell me. the line "if 
iterChildren==obj.children[-1]: break" does a nice job for me rite now (but 
only because obj.Children is never empty)

The reason I pasted all this code is to show that it can be helpful to have 
a nice value for a bool (iterator) (always returning 1 seems quite 
useless.) Hopefully some future version of python can have this additional 
change. (it should be trivial i think.....when StopIteration is thrown, a 
flag can probably be set in the iterator object???)

Chirayu.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020805/fef25dfc/attachment.html>


More information about the Python-list mailing list