[Tutor] about copy.copy

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Jul 20 19:33:36 CEST 2006


>> Do you have any questions so far about this?  Some kind of model like 
>> this is necessary to understand the situation you're seeing now, so 
>> please feel free to ask if any part of this is confusing.
>>
> But in the following example, a change in a spread to both b and c:
>>>> a=[[1,2,3], [4,5,6]]

Hi Linda,

Try drawing out a diagram for what things look like for:

     a = [[1, 2, 3], [4, 5, 6]]
     c = copy.copy(a)

It may help to explain the behavior that you're seeing.

Mutation is a very tricky thing!  It'll take some practice to get an 
intuition of what's happening.


(In fact, it is troublesome enough that some language communities try to 
avoid mutating things at all.  The ML and Haskell communities are examples 
of folks who try to live without mutation.  For the most part, they're 
perfectly happy without mutating their data structures.)


Best of wishes!


More information about the Tutor mailing list