On 10/03/2012 11:52 PM, Ed Owens wrote: > You are fundamentally correct about my confusion, though I'm trying to work > with tuples as the lowest level, which may not be relevant here. The tuples are an example of an immutable object. An immutable object (which contains only immutable objects) may be safely shared without risk of the kind of problem you're having. ints would have worked just as well.
Eryksun is almost correct, but I'm going to attempt to show you how to figure it out for yourself, next time. > > -----Original Message----- > . > > py> H = [[1, 2]] > py> J = [H[0]] > py> print H > [[1, 2]] > py> print J > [[1, 2]] Here, if you print id(H[0]) and id(J[0]) you'll see they're the same. You've already shown they're lists, and therefore mutable. Thus, you have a risk. H and J are different lists, each containing the same list as their content. Also try >>> H is J False >>> H[0] is J[0] True >>> Now, if we go one more level down, to H[0][0], we'll see a tuple. And although they're also identical, there's no problem there. This is how eryksun knew that a shallow copy was good enough. And for a shallow copy, using the slice notation is perfect. >>> J42 = [H[:1]] >>> print J42[0] [(1, 2)] >>> print id(J42[0]) 13964568 (Note that my shallow copy is not quite what eryksun was using. His lost one level of the nesting, by doing both the subscript and the slice.) Now, since the id is different, you won't get the problem you saw first. -- DaveA _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
