code doesn't work: question

Jay O'Connor joconnor at
Tue Sep 3 15:58:13 CEST 2002

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

>Python doesn't allow assignment inside expressions, the rationale (from
>the Python tutorial at
>- 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: [

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 O'Connor
joconnor at

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

More information about the Python-list mailing list