while row = c.fetchone(): # syntax error???
Alex Martelli
alex at magenta.com
Fri Aug 11 04:17:24 EDT 2000
"Thomas Gagne" <tgagne at ix.netcom.com> wrote in message
news:399370FD.F883EFCE at ix.netcom.com...
> At the top of a while, the value of the function c.fetchone() is what I
want
> to evaluate, but I need to save the return value. Whenever I try to code
this
> I get a syntax error--python doesn't seem to like the assignment in the
> conditional. No amount of parens seem to make a difference.
Right. Assignment is not an expression in Python. One benefit is that
you avoid such errors as coding "while x=y:" where you meant to code
"while x==y:". But it does mean that certain C idioms aren't Python idioms.
The workaround most often used is to change what one'd like to code as:
while row=c.fetchone(): # nope -- Python doesn't like this!
doit(row)
into the equivalent Python idiom:
while 1:
row=c.fetchone()
if not row: break
doit(row)
Alternatives abound, e.g:
class valueWrapper:
def set(self,value):
self.value=value
return value
row=valueWrapper()
while row.set(c.fetchone()):
doit(row.value)
Alex
More information about the Python-list
mailing list