if <assignment>:

Mel Wilson mwilson at the-wire.com
Sun Nov 24 11:11:43 EST 2002


In article <arqm0r$s6f$1 at maud.ifi.uio.no>,
=?ISO-8859-1?Q?Andr=E9_N=E6ss?= <andre.hates.spam at ifi.uio.no> wrote:
>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 :)

   We've just been there. groups.google.com can bring it all
back, although I don't offhand remember the subject.

   Basically, assignments aren't possible within
expressions.

   One good reason for this is list packing/unpacking in
assignments:

        a, b = 5, 8

   As it stands, this is an assignment which effectively makes
two 2-tuples equivalent, associating `a` with the value 5,
and `b` with the value 8.

   If `b=5` were an allowable expression, then the statement
above would change into a 3-tuple, and a useful piece of
semantics would be a tad more awkward.

   (Reading that thread, I was surprised to find that I'd
uncritically been associating `()` with tuples the way `[]`
are actually associated with lists.  'Tain't so.)

   You could think of making an exception for `if`
statements, but beyond an uncertain point, exceptions are
bad for a language; Gerald M.Weinberg's _The Psychology of
Computer Programming_ contains a discussion.  I don't know
where that point is, at most I might claim to know cases
where languages haven't passed it and cases where they have.

        Regards.        Mel.



More information about the Python-list mailing list