[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: expressions vs. statements



Michael Vanier <mvanier@bbb.caltech.edu> writes:

> Python actually has a glitch whereby "=" doesn't return a value
> normally but you are allowed to do
>
>   a = b = c = 0
>
> as a special case. 

It isn't a glitch, and "=" isn't returning any value there either.

That is just how the assignment statement works.  You assign a value
to one or more names.

> Guido is dead-set against it because he (rightfully) hates code like
> this:
>
>   if (a = getchar())
>       # do something

It isn't just that.  It's the bugs caused by unintentional use of "="
when the programmer wanted "==".

A fairly common and hard to diagnose type of bug in, for example, C:

    if (a = b)
        do something

The programmer probably wanted to test equality, but instead performs
an assignment and (probably) executes the body.  When "=" is not an
expression, this problem simply doesn't occur.

Bugs of this sort are notoriously hard to find when they occur in
complex programs.

The extra wordiness required by the lack of this feature is worth it, imo.

-Justin