while (a=b()) ...

Fredrik Lundh fredrik at pythonware.com
Tue May 11 17:25:38 EDT 1999


scott cotton <scott at chronis.pobox.com> wrote:
> >c=curs.fetchone()
> >while c:
> >    do something with c
> >    c=curs.fetchone()
> >
> >Is it a little redundant? Yes.  Error prone?  No.
> 
> Error prone it is - by virtue of being redundant. changes to
> the loop may require changing two code lines, and it's easy
> to forget to update redundant code.

so don't use it. use the Standard Python Idiom
That Everyone Else Uses instead (see below).

> while (c=curs.fetchone(); c):
> 
> is not redundant, nor does it have the '=' != '==' problem.

it has one big problem: it's impossible to understand.
doesn't look like any other mainstream programming
language (as is the case for most Python features),
and definitely doesn't look like anything a non-pro-
grammer have ever seen (like prominent Python
features such as indentation, colon after if/while,
etc).  if we really need a special syntax for this (no,
we don't), I've seen better proposals...

but I still claim that this whole issue is just a big
time sink.  just READING a single post on this topic
(including this one) takes more time than you'll
ever spend typing:

    while 1:
        c = curs.fetchone()
        if not c:
            break
        # process c

instead of any sugared version of this idiom.

...and don't tell me that anyone smart enough to operate
a contemporary computer cannot train her/his brain to
quickly identify the above as an instance of a commonly
used pattern, rather than a number of individual state-
ments whose purpose needs to be carefully analyzed one
by one...

face it: this idiom is used all over the place (found some
80 places in the standard library, for example), so you
need to learn it anyway if you're ever going to look at
code written by anyone else.  and when you've done
that, you might as well use it yourself.

now, has anyone written any useful Python code today?

</F>





More information about the Python-list mailing list