while (assignment):

Michele Simionato mis6 at pitt.edu
Wed Jul 30 14:19:07 CEST 2003


Sybren Stuvel <sybrenUSE at YOURthirdtower.imagination.com> wrote in message news:<slrnbieppo.86l.sybrenUSE at sybren.thirdtower.com>...
> Hannu Kankaanpää enlightened us with:
> > Typically this is done by breaking out of the loop:
> > 
> > while True:
> >     info = mydbcursor.fetchone()
> >     if not info:
> >         break
> >     print "Information: "+str(info)
> 
> This was my first solution as well. Kinda ugly, though.
> 
> Sybren

When I started using Python I though the "while 1" idiom was horrible.
Now I think it is quite Pythonic. Few months can change your mind! ;)

You may also use this form, which you may find more readable:

while 'info': 
     info = mydbcursor.fetchone()
     if not info: break
     print "Information: "+str(info)

Remember, 'info' is another spelling of True!
This is a trick anyway, and I always use "while 1" in my code, it is
the idiomatic way at the end.

                        Michele




More information about the Python-list mailing list