code doesn't work: question

Jay O'Connor joconnor at cybermesa.com
Tue Sep 3 09:58:13 EDT 2002


On 03 Sep 2002 12:28:59 +0200, Geert-Jan Van den Bogaerde
<gvdbogae at vub.ac.be> wrote:

>Python doesn't allow assignment inside expressions, the rationale (from
>the Python tutorial at http://www.python.org/doc/current/tut/node7.html
>- section 5.5 last sentence): 
>
>"""
>Note that in Python, unlike C, assignment cannot occur inside
>expressions. C programmers may grumble about this, but it avoids a
>common class of problems encountered in C programs: typing = in an
>expression when == was intended. 
>"""
>
>Given the number of times I've personally made this mistake, I'd agree
>with Python's rationale for leaving this out.


This is really only a problem in a language that doesn't really have a
boolean data type and then allows boolean evaluation of non-boolean
objects (such as C with numbers and pointers and Python with..almost
everything :)

Smalltalk allows assignment within an expression, but does not suffer
from the "I meant == and accidentally typed =" problem because
Smalltalk has a first class Boolean class and does not allow boolean
comparisons for non-boolean objects

(x :=y) ifTrue: [
	"blah"
]

Only works if y is already a Boolean; if it's an Integer, the ifTrue:
message raises an error, so 

((x := y) == 5) ifTrue: [

]

is perfectly ok, and much safer than the equivalent C


Take care,
Jay
Jay O'Connor
joconnor at cybermesa.com
http://www.cybermesa.com/~joconnor


"God himself plays on the bass strings first, when he tunes the soul"



More information about the Python-list mailing list