variable assignment in "while" loop

Peter Hansen peter at engcorp.com
Tue Jul 29 09:23:17 EDT 2003


Sybren Stuvel wrote:
> 
> Is it possible to use an assignment in a while-loop? I'd like to do
> something like "loop while there is still something to be read, and if
> there is, put it in this variable". I've been a C programmer since I
> was 14, so a construct like:
> 
> while info = mydbcursor.fetchone():
>         print "Information: "+str(info)
> 
> comes to mind. Unfortunately, this doesn't work. Is there a similar
> construct in python?

Yes:

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

-Peter




More information about the Python-list mailing list