C-like assignment expression?

Antoon Pardon apardon at forel.vub.ac.be
Thu May 22 05:46:23 EDT 2008


On 2008-05-21, Diez B. Roggisch <deets at nospam.web.de> wrote:
> boblatest at googlemail.com wrote:
>
>> On May 21, 1:47 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
>> 
>>> Although that solution is pretty, it is not the canonical solution
>>> because it doesn't cover the important case of "if" bodies needing to
>>> access common variables in the enclosing scope.  (This will be easier
>>> in Python 3 with 'nonlocal', though.)  The snippet posted by Diez is
>>> IMHO closer to a canonical solution to this FAQ.
>> 
>> Hello everybody,
>> 
>> thanks for the various answers. I'm actually pretty puzzled because I
>> expected to see some obvious solution that I just hadn't found before.
>> In general I find Python more elegant and syntactically richer than C
>> (that's where I come from), so I didn't expect the solutions to be a
>> lot more verbose and/or ugly (no offense) than the original idea which
>> would have worked if Python's assignment statement would double as
>> expression, as in C.
>
> Well, it's a design-decision - and I'm pretty ok with it being a bit verbose
> here - as it prevents a *great* deal of programming errors that would
> otherwise happen from accidentally writing a = b where a == b was meant.

But that is an error that occurs because of the specific symbols chosen
to represent an assignment or equality test. At the time python was
first designed, other symbols for assignment were already used for
in other languages, like := and <-

So if preventing errors was the main motivation why not allow an
assignment to be an expression but use a symbol for the assignment
that would prevent those kind of errors?

I find it hard to believe that a design choice like whether or not
to have the assignment behave as an expression or not, was decided
on the ground of a particulare lexical representation of the assignment
symbol.

-- 
Antoon Pardon



More information about the Python-list mailing list