Multiple assignment and the expression on the right side

Alex Martelli aleaxit at yahoo.com
Tue Feb 21 11:12:12 EST 2006


Suresh Jeevanandam <jm.suresh at gmail.com> wrote:
   ...
> I think I got confused because of "Each time" in the sentence which 
> gives a feeling that it gets executed several times. Maybe, It could 
> have been just written, "When the statement gets executed, the right 
> hand side is evaluated once, and the result is assigned to each of the
> target".

Ah, but that suggests to *ME* that if you have the statement in a loop,
the RHS executes only once (the first time).  Let's try:

"""
Each time the statement executes, the RHS expression is evaluated just
once, no matter how many targets are part of the statement. Each target
then gets bound to the single object returned by the expression, just as
if several simple assignments executed one after the other. 
"""

[[I'm using LHS and RHS in the 2nd edition, for extra conciseness, after
introducing the acronyms just once where I first use them]].

This seems to cover the bases:

a. RHS is evaluated EACH TIME the statement executes,
b. but for each such time, RHS is evaluated JUST ONCE.

and the addition of the "no matter" clause should pound the nail into
the coffin.  What do y'all think?


Thanks for the comments, by the way, and my apologies for reacting to
them rather snappily at first blush -- I had just spent a long weekend
mostly slaving on the 2nd edition, so I guess I was irritable, but
that's no excuse for my discourtesy: sorry. Comments and criticisms on
my work are in fact always welcome (submitting them as errata on
O'Reilly's site ensures I will see them, which isn't certain here),
since they give me the best chance to enhance the work's quality (it's
quite common for the author of some text to fail to see potential issues
with the text, which may trip some readers but appears to be perfect to
the author -- that's why two heads are better than one, and N>2 even
better than two!-).


So, again, thanks.

Alex



More information about the Python-list mailing list