By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,028 Members | 986 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,028 IT Pros & Developers. It's quick & easy.

Multiple assignment and the expression on the right side

P: n/a
Dear all,
I read in "Python in a Nutshell" that when we have multiple assignments
made on a single line, it is equivalent to have those many simple
assignments and that the right side is evaluated once for each
assignment. [The wordings are mine. I am not sure if this is what he
intended].

So, In the following code snippet I expected the values of c, d, e to
be different. But they are not? I am missing something... What is it?
a = 12
def x(): global a; a+=1; return(a) .... x() 13 a 13 c = d = e = x()
c 14 d 14 e 14


Thanks.
regards,
Suresh
Feb 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Suresh Jeevanandam wrote:
Dear all,
I read in "Python in a Nutshell" that when we have multiple assignments
made on a single line, it is equivalent to have those many simple
assignments and that the right side is evaluated once for each
assignment. [The wordings are mine. I am not sure if this is what he
intended].


No, it isn't. He says, "Each time the statement executes, the right-hand side
expression is evaluated once." "[T]he statement" refers to the multiple
assignment as a whole.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Feb 21 '06 #2

P: n/a
Suresh Jeevanandam <jm*******@gmail.com> wrote:
Dear all,
I read in "Python in a Nutshell" that when we have multiple assignments
made on a single line, it is equivalent to have those many simple
assignments and that the right side is evaluated once for each
assignment. [The wordings are mine. I am not sure if this is what he
intended].
Since the original text is:

"""
Each time the statement executes, the right-hand side expression is
evaluated once. Each target gets bound to the single object returned by
the expression.
"""

it might be interesting to understand how you managed to translate ONCE
into ONCE PER ASSIGNMENT TARGET. I can most earnestly assure you that
when I wrote ONCE I meant ONCE. If ONCE meant TWICE (or more), how could
there be a guaranteed SINGLE OBJECT to which each target gets bound?
So, In the following code snippet I expected the values of c, d, e to
be different. But they are not? I am missing something... What is it?


A good command of English, maybe? With that ONCE and SINGLE in there,
no matter how much I try to, I just cannot see ambiguity in the words I
had written. Nevertheless, in your honor, I guess I will redundantly
change the ONCE into JUST ONCE in the second edition (I do strive for
maximum conciseness in the Nutshell, but I guess I can spare one extra
four-letter word, even though this is the only time I ever heard anybody
express any doubt or misunderstanding about this paragraph).
Alex
Feb 21 '06 #3

P: n/a
While I think that the paragraph is correct still there is IMO indeed
the (low) risk of such a misunderstanding. The problem is that "the
statement executes" can IMO easily be understood as "the statements
execute" (especially if your background includes only languages where
there's no multiple assignment) and the world "single" is also
frequently used in phrases like "every single time" where can indeed
denote a context of plurality.
Adding "just" or "only" would be IMO an great improvement by focusing
the attention on the key point, as it would be IMO better using
"rightmost" instead of "right-hand" (in this case even saving a char
;-) )

Just two foreign cents

Feb 21 '06 #4

P: n/a
Alex Martelli wrote:
Suresh Jeevanandam <jm*******@gmail.com> wrote:
Dear all,
I read in "Python in a Nutshell" that when we have multiple assignments
made on a single line, it is equivalent to have those many simple
assignments and that the right side is evaluated once for each
assignment. [The wordings are mine. I am not sure if this is what he
intended].
Since the original text is:

"""
Each time the statement executes, the right-hand side expression is
evaluated once. Each target gets bound to the single object returned by
the expression.
"""


Alex,
I should have read carefully.

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".

Thanks a lot.

regards,
Suresh
it might be interesting to understand how you managed to translate ONCE
into ONCE PER ASSIGNMENT TARGET. I can most earnestly assure you that
when I wrote ONCE I meant ONCE. If ONCE meant TWICE (or more), how could
there be a guaranteed SINGLE OBJECT to which each target gets bound?
So, In the following code snippet I expected the values of c, d, e to
be different. But they are not? I am missing something... What is it?


A good command of English, maybe? With that ONCE and SINGLE in there,
no matter how much I try to, I just cannot see ambiguity in the words I
had written. Nevertheless, in your honor, I guess I will redundantly
change the ONCE into JUST ONCE in the second edition (I do strive for
maximum conciseness in the Nutshell, but I guess I can spare one extra
four-letter word, even though this is the only time I ever heard anybody
express any doubt or misunderstanding about this paragraph).
Alex

Feb 21 '06 #5

P: n/a
Suresh Jeevanandam <jm*******@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
Feb 21 '06 #6

P: n/a
On Tue, 21 Feb 2006 10:53:21 +0530
Suresh Jeevanandam <jm*******@gmail.com> wrote:
I read in "Python in a Nutshell" that when we have
multiple assignments
made on a single line, it is equivalent to have those many
simple assignments and that the right side is evaluated
once for each assignment. [The wordings are mine. I am
not sure if this is what he intended].

>>> c = d = e = x()


AFAIK, this is equivalent to this:

e = x()
d = e
c = d

So, in fact, what you say is true, but these, of course will
not evaluate x multiple times.

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Feb 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.