[Edu-sig] Fw: Python sequences by reference - how to make

Kirby Urner urnerk@qwest.net
Wed, 25 Sep 2002 07:15:01 -0700


At 08:57 AM 9/25/2002 -0400, Arthur wrote:

>Hate to think that Martelli's suggestion that math types might need to stick
>with languages that implement a more uniformly functional model, is the only
>answer to this issue.
>
>Art

re: http://mail.python.org/pipermail/python-list/2002-September/123526.html

Martelli's answer was quite interesting, as it mixes in a lot of history
about computer languages.  His main point is that assignment by reference,
vs. assignment by copy, is the trend in modern languages, and being stuck
on a copy-based semantics is not intrinsically a "math thing" -- it's
just a habit of thought.

This quote seems to argue against your suggestion:

"You're in water. Water is *everywhere*. When you think there
is nothing there, there's water. Just get used to it. Trying
to muddy up the water in an attempt to make it more visible
would be counter-productive." [fixed a typo]

I think when we teach Python, the assignment-by-reference idea has to
be made very clear:

   >>> a = [1, 2, 3]
   >>> thelist = [a,a,a]  # same as [a]*3
   >>> a[0]=42
   >>> thelist            # there's only one list in this picture
   [[42, 2, 3], [42, 2, 3], [42, 2, 3]]
   >>> b = a              # b also points to a's list

   >>> id(a)     # id() could be used when showing the identity
   11095040

   >>> id(b)
   11095040

   >>> a[0] = 92
   >>> thelist = [a,b,a]  # still only one list here
   [[92, 2, 3], [92, 2, 3], [92, 2, 3]]

So how do we get another copy of a to play with?

   >>> from copy import copy
   >>> b = copy(a)         # now b points to its own list
   >>> a[1] = 17
   >>> a
   [92, 17, 3]
   >>> b        # b is unchanged
   [92, 2, 3]

So back to our original example (with copy still available):

   >>> a = [1,2,3]
   >>> thelist = [a,copy(a),copy(a)]
   >>> a[0] = 42
   >>> thelist
   [[42, 2, 3], [1, 2, 3], [1, 2, 3]]

Stuff like that.

I think one could argue that "teaching by changing the list of
built-ins" is too subtle to really make newbies notice.  Beginners
don't necessarily know how to get a list of what's built in.
How would they know 'copy' is even present?

They rely on a book to tell them, which book then goes through each
feature.

But if you're relying on a book or tutorial, then the same source
might as well give examples of what's *not* a built in, e.g. copy.

I'd think the math people would be especially easy to educate:
just as you must import sin, cos, log, so must you import copy,
i.e. lots of functionality is in the libraries (same with C,
same with Java, same all modularized languages).

Kirby