Multiple assignment and the expression on the right side

S

Suresh Jeevanandam

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?
Thanks.
regards,
Suresh
 
R

Robert Kern

Suresh said:
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
(e-mail address removed)

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

Alex Martelli

Suresh Jeevanandam said:
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
 
A

Andrea Griffini

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
 
S

Suresh Jeevanandam

Alex said:
Suresh Jeevanandam said:
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
 
A

Alex Martelli

Suresh Jeevanandam said:
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
 
T

Terry Hancock

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top