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?[color=blue][color=green][color=darkred]
>>> a = 12
>>> def x(): global a; a+=1; return(a)[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> x()[/color][/color][/color]
13[color=blue][color=green][color=darkred]
>>> a[/color][/color][/color]
13[color=blue][color=green][color=darkred]
>>> c = d = e = x()
>>> c[/color][/color][/color]
14[color=blue][color=green][color=darkred]
>>> d[/color][/color][/color]
14[color=blue][color=green][color=darkred]
>>> e[/color][/color][/color]
14[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
Thanks.
regards,
Suresh
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?[color=blue][color=green][color=darkred]
>>> a = 12
>>> def x(): global a; a+=1; return(a)[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> x()[/color][/color][/color]
13[color=blue][color=green][color=darkred]
>>> a[/color][/color][/color]
13[color=blue][color=green][color=darkred]
>>> c = d = e = x()
>>> c[/color][/color][/color]
14[color=blue][color=green][color=darkred]
>>> d[/color][/color][/color]
14[color=blue][color=green][color=darkred]
>>> e[/color][/color][/color]
14[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
Thanks.
regards,
Suresh
Comment