while (a=b()) ...

Moshe Zadka moshez at math.huji.ac.il
Tue May 11 14:53:09 EDT 1999


On Mon, 10 May 1999, Andrew Csillag wrote:

> scott cotton wrote:
> > 
> > On 9 May 1999 19:14:10 GMT, Andrew Clover wrote:
> > >Nathan Clegg (nathan at islanddata.com) wrote:
> > >
> > >> The ideal, of course, would be:
> > >
> > >> while (c = curs.fetchone()): ...
> > >
> > >> That is my only complaint about python, that statements cannot be
> > >> expressions.
> 
> In my opionion, this a feature, not a bug.  I cannot remember how many
> times I accidentally typed c = NULL instead of c == NULL in C.
> 
> The problem with C is that it gives you much power, but enoughrope to
> hang yourself as well as many of your neighbors.

Indeed. I suggest a different fix: real test-in-the-middle loops.

Something like

do:
	line=sys.stdin.readline()
while line:
	sys.stdout.write(line)

This actually does the Right Thing(TM) -- read a line, and as long
as the line is not empty, print it out. 

This can even be used to make test-at-end loops

do:
	something
while something_else: pass

It is certainly, IMNSHO, better then:

> c=curs.fetchone()
> while c:
>   do something with c
>   c=curs.fetchone()
> 
> Is it a little redundant? Yes.  Error prone?  No.

Yes it is error prone. Say, no you have an array of ``curs'' objects

Change it to 

c=curs[0].fethone()
while c:
	dom something with c
	c=curs.fetchone()

(Ploops! forgot to change in two places: error prone!)
--
Moshe Zadka <mzadka at geocities.com>. 
QOTD: My own exit is more likely to be horizontal then perpendicular.






More information about the Python-list mailing list