while row = c.fetchone(): # syntax error???

Richard Brodie R.Brodie at rl.ac.uk
Mon Aug 14 04:51:05 EDT 2000


"Alex Martelli" <alex at magenta.com> wrote in message news:8n0d1t0kao at news2.newsguy.com...

> 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)

The real problem is the underlying class has a 'C' like API. Returning
magic numbers like 0 isn't really Python's style. The natural way to do
it in Python (given a more suitable underlying class) is:

for row in c.fetchrows():
    doit(row)

As noted, you can always wrap the class. But it's not really Python's
fault that it was broken in the first place.





More information about the Python-list mailing list