if <assignment>:

David Brown david at no.westcontrol.spam.com
Mon Nov 25 03:47:47 EST 2002


"André Næss" <andre.hates.spam at ifi.uio.no> wrote in message
news:arqm0r$s6f$1 at maud.ifi.uio.no...
> When I started learning Python one of the things that surprised me was
that
> you couldn't do assignments inside the if clause, e.g.:
>
> if myvar = someFunction():
>
> My question is, what is the rationale for this? Is it a technical issue?
Or
> purely a matter of language design? I'm curious because I'm interested in
> the design og programming languages, not because I want this behavior
> changed in Pyton :)
>
> Thanks.
> André Næss
>

My guess is that the languages you are most familiar with are C and C++.
The question should not be "why doesn't Python support 'if myvar =
someFunc()'?", but "why *does* C support 'if (myvar = someFunc())' ?"  The
answer is that C was designed with the sole aim of reducing keystrokes for
the programmer, regardless of its effect on program readability and
correctness.  This particular "feature" is a huge source of bugs and
frustration in C programming - mixing up "=" and "==" in conditions is
probably the biggest single cause of C program bugs.  Python, on the other
hand, is designed for improving program readability and correctness, even if
it occasionally (actually seldom in practice) requires more keystrokes.






More information about the Python-list mailing list