On Tue, Nov 4, 2008 at 9:03 AM, Stan Schymanski <[EMAIL PROTECTED]> wrote: > > Thanks a lot, Justin! That's good to know. Incidentally, isn't it a bit > inconsistent to have different behaviour for assignments related to > lists and dictionaries than those related to symbolic variables?
The behavior is identical -- both assignments create a new reference, as do *all* assignments in Python. The difference is merely that lists and dictionaries are immutable whereas symbolic variables aren't. William > > Example: > > sage: L1=[1,2,3];L2=[3,4,5] > sage: L=[L1,L2] > sage: L > [[1, 2, 3], [3, 4, 5]] > sage: LL=L > sage: LL[1]=0 > sage: LL > [[1, 2, 3], 0] > sage: L > [[1, 2, 3], 0] > sage: LL=3 > sage: L > [[1, 2, 3], 0] > sage: LL > 3 > > After setting LL=L, any changes to elements in LL lead to similar > changes in L, but if I change LL as a whole, the association between LL > and L breaks down. > > For symbolic variables LL=L does not lead to a link between the two: > > sage: L=1 > sage: LL=L > sage: LL > 1 > sage: L=2 > sage: LL > 1 > sage: L > 2 > > The retrospective links between lists can get really confusing: > > sage: L1=[1,2,3];L2=[3,4,5] > sage: L=[L1,L2] > sage: L > [[1, 2, 3], [3, 4, 5]] > sage: L1[0]=99 > sage: L > [[99, 2, 3], [3, 4, 5]] > sage: L[0][1]=88 > sage: L > [[99, 88, 3], [3, 4, 5]] > sage: L1 > [99, 88, 3] > > If I construct a list out of two other lists, I usually don't expect the > original lists to change if I manipulate the resulting list. > How can I break such links? Why does LL=L mean that L=LL for lists and > dictionaries but not for variables? What is the use of it anyway?? I'm > really confused now. > > The retrospective link does not work if I use any operators such as > L=L1+L2 or L=2*L1. > > Thanks for pointing me to this! > > Cheers > Stan > > Justin C. Walker wrote: >> Hi, Stan, >> >> On Nov 4, 2008, at 00:47 , Stan Schymanski wrote: >> >> >>> Sorry, I should have looked around a bit more. If I replace pars1 = >>> pars >>> by pars1 = pars.copy() in the below code, the two dictionaries are >>> independent. >>> >> >> >> You found part of the story, but there's a bit more. Consider this >> example: >> >> sage: L1=[1,2,3];L2=[3,4,5] >> sage: L=[L1,L2] >> sage: L3=[6,7,8] >> sage: LL=copy(L) >> sage: LL[1]=L3 >> sage: L >> [[1, 2, 3], [3, 4, 5]] >> sage: LL >> [[1, 2, 3], [6, 7, 8]] >> sage: LL[0][1]=17 >> sage: LL >> [[1, 17, 3], [6, 7, 8]] >> sage: L >> [[1, 17, 3], [3, 4, 5]] >> >> In your example, only the dictionary associations are copied. If one >> of the values (or keys, FWIW) in one of the entries had deeper >> structure, that would not get copied (only a pointer would be copied). >> >> 'copy()' just copies the "first level"; if you want a copy of the >> entire object, you need to use 'deepcopy()'. >> >> HTH >> >> Justin >> >> -- >> Justin C. Walker, Curmudgeon at Large >> Institute for the Absorption of Federal Funds >> ----------- >> Like the ski resort full of girls hunting for husbands >> and husbands hunting for girls, the situation is not >> as symmetrical as it might seem. >> - Alan MacKay >> -- >> >> >> > >> > > > > > > -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
