Newbie question about reference

Greg Ewing (using news.cis.dfn.de) me at privacy.net
Mon Mar 24 07:21:46 CET 2003


Tim Smith wrote:
> OK, I had thought that L = [x,y,z] was binding the list L as an
> ordered set of references/pointers to the variables x, y, z.

No, it contains copies of the references currently bound to
those names at the time the list was created. Rebinding
those names later doesn't change which references the
list contains.

> Obviously
> not, as integers are, along with strings etc., immutable types.

That's a red herring in this case. The fact that x, y and z
happened to be bound to immutable objects doesn't come into
it. The same thing happens if they're bound to mutable objects,
e.g.

 >>> x = [1, 2]
 >>> y = [3, 4]
 >>> z = [5, 6]
 >>> l = [x, y, z]
 >>> print l
[[1, 2], [3, 4], [5, 6]]
 >>> x = [7, 8]
 >>> print l
[[1, 2], [3, 4], [5, 6]]

> This is not clearly explained in the material I have read so far.

Sadly, it's all too common for introductory Python material to
do a poor job of conveying the right mental model. Doing so
unambiguously is very difficult using words alone, and really
requires the use of diagrams. Here's an attempt...

Before creating the list:

   +----+           +---+
x | *------------->| 1 |
   +----+           +---+

   +----+           +---+
y | *------------->| 2 |
   +----+           +---+

   +----+           +---+
z | *------------->| 3 |
   +----+           +---+


After creating the list:


   +----+                                +---+
x | *---------------------------------->| 1 |
   +----+             /                  +---+
                      |
   +----+             |                  +---+
y | *----------------|----------------->| 2 |
   +----+             |   /              +---+
                      |   |
   +----+             |   |              +---+
z | *----------------|---|------------->| 3 |
   +----+             |   |   /          +---+
                      |   |   |
   +----+           +-|-+-|-+-|-+
l | *------------->| * | * | * |
   +----+           +---+---+---+


After assigning to y:


   +----+                                +---+
x | *---------------------------------->| 1 |
   +----+             /                  +---+
                      |
   +----+   +---+     |                  +---+
y | *----->| 0 |     |   /------------->| 2 |
   +----+   +---+     |   |              +---+
                      |   |
   +----+             |   |              +---+
z | *----------------|---|------------->| 3 |
   +----+             |   |   /          +---+
                      |   |   |
   +----+           +-|-+-|-+-|-+
l | *------------->| * | * | * |
   +----+           +---+---+---+


As you can see, the list still contains references to the
same objects as it did before, but y contains a reference
to a different object.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list