473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Multiple assignment and the expression on the right side

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
6 3832
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
19
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A...
13
by: Daniel W | last post by:
Hi! I tried to post this to comp.lang.c.moderated but it didn't seem to go through. I've got a question about volatiles in assignment expressions. I found the following code snippet in an...
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
35
by: nagy | last post by:
I do the following. First create lists x,y,z. Then add an element to x using the augumented assignment operator. This causes all the other lists to be changed also. But if I use the assignment...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.