[Edu-sig] how to make clear: understand references

Douglas S. Blank dblank@cs.brynmawr.edu
Fri, 20 Sep 2002 16:24:48 -0400


Earlier, I spake:

>>I would assume that copy() would be a deepcopy by default (as that
>>would be what new users would assume). So, that might suggest a
>>shallowcopy() method too.

which prompted a certain Guido van Rossum to comment:

> Deep copying is not very useful, as it usually copies way too much.

Upon further reflection, I think I was wrong: the default should not 
always be a deep copy. Just think of copying a node from the middle of a 
tree--- you don't want to copy the whole subtree, just that node. But it 
does seem to be the right thing to do for arrays.

> If you focus all your examples on multi-dimensional arrays, yes, then
> you'd need deep copying to be the default.  But I think that is a
> narrow-minded view on learning to program, based in the state of the
> art in the early '60s (when arrays were the only data structuring
> methodology available in programming languages).

I did give two examples of multi-dimensional arrays, but the issue, I 
believe, is with the operators, not the arrays. I had suggested:

 >>> a = [[0] * 5].copy() * 3

But, I now see that that doesn't fix the problem that I originally had, 
because, even a deepcopy doesn't help:

 >>> a = [deepcopy([0] * 6)] * 3    # or
 >>> a = deepcopy([[0] * 6]) * 3
 >>> a[0][0] = 4
 >>> a
[[4, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0]]

So now we see that this particular confusion won't (easily) go away with 
anything other than understanding where to draw those little boxes 
around objects and understanding references.

-Doug

> --Guido van Rossum (home page: http://www.python.org/~guido/)
> 
> _______________________________________________
> Edu-sig mailing list
> Edu-sig@python.org
> http://mail.python.org/mailman/listinfo/edu-sig



-- 
Douglas S. Blank,         Assistant Professor
dblank@brynmawr.edu,            (610)526-6501
Bryn Mawr College,   Computer Science Program
101 North Merion Ave,       Park Science Bld.
Bryn Mawr, PA 19010  dangermouse.brynmawr.edu