[Tutor] Re: Multiple Assignments...

Bruce Sass bsass@freenet.edmonton.ab.ca
Fri, 11 May 2001 15:33:24 -0600 (MDT)


On Thu, 10 May 2001, Praveen Pathiyil wrote:

> Hi Danny,
>         Thx for the explanation. I am just prolonging the discussion.....
>
> a,b = b,a
>
> In your words, this would be
> a, b = some_right_hand_side_value
>
> I was wondering abt the sequence after that.
> say a = 'A' and b = 'B' initially.
>
> If the assignment a = 'B' happens first, then won't the assignment for b
> will be 'B' itself as the value referenced by "a" on the RHS will be changed
> ?
>
> Or is it that all the objects on the RHS are replaced by their values before
> the assignment happen ?
>
> i.e, a, b = 'B', 'A'
>
> Just making sure !!!

When you recognize that this (below), and what Danny wrote, are
really saying the same thing, you got it...

Doing "a,b = 0,1" sets things up so that:

	a --> 0
	b --> 1

a and b are labels, 0 and 1 are objects

Doing "a, b = b, a" has the language going through this process:
(where *0 and *1 are pointers to the 0 and 1 objects)

	b, a --> (*1, *0)	# tupple packing

	(*1, *0) --> a, b	# tupple un-packing

which results in:

	a --\  /--> 0
	     \/
	     /\
	b --/  \--> 1

The RHS results in a new tuple (the temp, if you like), which is then
assigned to the LHS -- much different from a serial "a = b ; b = a",
you had in mind, eh.


- Bruce