while(s=infile.readline()): # reading input

Michael P. Reilly arcege at shore.net
Mon Jun 21 17:39:31 EDT 1999


Angus MacKay <amackay at starvision.com> wrote:

: why can't I have syntax like this?
: python is written in C after all isn't it?

: this works fine:
:   while infile.readline():
:     print "Hi!"

: this doe not:
:   while s = infile.readline():
:     print s

: which is okay, but alas this does not work:
:   while(s=infile.readline()):
:     print s

: is there any way to avoid using the ugly:
:   while 1:
:     s = infile.readline()
:     if not s: break
:     print s

Please read the FAQ 6.30 "Why can't I use an assignment in an expression?"
(http://grail.cnri.reston.va.us/cgi-bin/faqw.py?req=show&file=faq06.030.htp).
You can also read numerous discussions on this question from past years
on Dejanews (http://www.dejanews.com/).

As for your other question about chomp; this question was also asked in
this newsgroup a year or two ago, you can probably find that discussion
on Dejanews as well.  One basic answer is the difference between CISC
and RISC: CISC stands for "complex instruction set computer" and RISC
is "reduced instruction set computer".  Perl is CISC-oriented, and has
specialized functions and operations like chomp();  Python is
RISC-oriented, giving you a simplier, more flexible language, at the
perceived expense of not having "nifty functions."

For that reason and many, many others, "things" like assignment
expressions and specialized core language constructs just aren't there
in Python (there are always exceptions tho).  Then there is Python 2.0
coming in the future (don't ask when); and we trust Guido not to make
the mistake of Perl5.

  -Arcege





More information about the Python-list mailing list