variable assignment in "while" loop
Bengt Richter
bokr at oz.net
Tue Jul 29 15:22:50 EDT 2003
On 29 Jul 2003 12:08:50 GMT, Sybren Stuvel <sybrenUSE at YOURthirdtower.imagination.com> wrote:
>Hi there,
>
>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?
>
I thought of a little variation on the list comprehension hack that is usually advised against:
while [info for info in [mydbcursor.fetchone()] if info]:
print "Information: %s" % info
I sort of like the mnemonic of the if info that makes the while
see [] at the end rather than e.g. [None][0]
Regards,
Bengt Richter
More information about the Python-list
mailing list