readline in while loop

Jay O'Connor joconnor at nets.com
Fri May 23 12:20:04 EDT 2003


In article <mailman.1053699295.2965.python-list at python.org>, "Steven
Taschuk" <staschuk at telusplanet.net> wrote:


> Python distinguishes between statements and expressions.
>     l = f.readline()
> is a statement (an assignment statement, to be specific), but
>     while ... :
> requires an expression.  (This is unlike C; one reason Python works this
> way is to avoid this:
>     while (x = 5) {  /* oops -- meant x == 5 */
> This kind of error is common in C, and can be difficult to find. Python
> helpfully tells you immediately that something is wrong.)

Smalltalk allows such assignments but avoids the problem that C has, by
having a true boolean class.

	comparision

	[x = 5] whileTrue: [

	]

This works because the comparison of x = 5 returns a boolean object that
understands the message #whileTrue: 

	[x := 5] whileTrue: [

	]

This is syntacically allowed, but will raise an error because the
assigment x:=5 returns a number, which does not understand #whileTrue: 

Smalltalk does not use numbers,or anything else, as booleans so it avoids
the various errors that come up when you mix assignments in expressions
in languages that also use numbers as booleans.

FWIW, I know Ada has a true boolean type and avoids the C problems as
well.

-- 
Jay O'Connor
http://www.r4h-music.com

"God Himself plays the bass strings first,
when He tunes the soul"




More information about the Python-list mailing list