variable assignment in "while" loop
Aahz
aahz at pythoncraft.com
Tue Jul 29 10:21:39 EDT 2003
In article <slrnbicoqq.6gh.sybrenUSE at sybren.thirdtower.com>,
Sybren Stuvel <sybrenUSE at YOURthirdtower.imagination.com> 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?
Peter usually gives a good answer, but this time there's a better
answer:
def fetch_iter(cursor):
info = True
while info:
info = cursor.fetchone()
yield info
for info in fetch_iter(mydbcursor):
print "Information:" + str(info)
Generally speaking, any time you want to do assignment as part of a
while loop, you really want to convert it a for loop.
BTW, DO NOT USE TABS in Python programs. You WILL regret it.
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
More information about the Python-list
mailing list