[Tutor] Still confused about Python references/objects

Bruce Sass bsass@freenet.edmonton.ab.ca
Sat, 31 Mar 2001 13:25:04 -0700 (MST)


On Sat, 31 Mar 2001, Sheila King wrote:
<...>
> Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.6 -- press F1 for help
> >>> x = 2
> >>> y = 3
> >>> print x, " ", y
> 2   3
> >>> (y, x) = (x, y)
> >>> print x, " ", y
> 3   2
> >>> def swap(a, b):
> 	(b, a) = (a, b)

You are assigning inside a function, which creates local variables...

> >>> swap(x, y)
> >>> print x, " ", y
> 3   2
> >>>
>
> Could someone explain this to me? Why doesn't the swap function do anything?

...so "x" and "y" are not the same as "a" and "b", and neither "a" or
"b" ever get out of the function to affect anything.


- Bruce