From: sdm7g at virginia.edu (Steven D. Majewski)
Date: Wed, 12 May 1999 00:59:56 GMT
Subject: while (a=b()) ...
In-Reply-To: <033101be9bf4$d1ed7c00$f29b12c2@pythonware.com>
References: <033101be9bf4$d1ed7c00$f29b12c2@pythonware.com>
Message-ID: <Pine.A32.3.90.990511205539.21296B-100000@elvis.med.Virginia.EDU>
Content-Length: 2920
X-UID: 1733                                                

On Tue, 11 May 1999, Fredrik Lundh wrote:

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


OK -- new suggested idiom for those folks who Really, Really have
to have it all in one statement (It *could* be on one line, but you
wouldn't be able to read it):


import sys,string

while setattr( sys.modules[__name__], 'LINE', 
		getattr( sys.modules[__name__], 'FILE',
		  setattr( sys.modules[__name__], 'FILE', 
		    getattr( sys.modules[__name__], 'FILE', 
		      open('ReadMe')))).readline()) or LINE: print string.strip(LINE)




---|  Steven D. Majewski   (804-982-0831)  <sdm7g at Virginia.EDU>  |---
---|  Department of Molecular Physiology and Biological Physics  |---
---|  University of Virginia             Health Sciences Center  |---
---|  P.O. Box 10011            Charlottesville, VA  22906-0011  |---

    Caldera Open Linux: "Powerful and easy to use!" -- Microsoft(*)
     (*) <http://www.pathfinder.com/fortune/1999/03/01/mic.html>
 





