Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Variable assignment in liss

This is a very basic question because I am a new user of SAGE with a background mostly in Java and C++. But in the following session: sage: var('A') A sage: var('B') B sage: for item in alist: item = B print item ....: B sage: for item in alist: print item ....: A I am not sure why the value of the item in the list is printed as 'A' instead of 'B'. During the previous loop it seems like it assigned the value 'B' and printed out that the item was 'B. This may be an issue of the line 'item = B' returning a transient copy of the item, instead of a reference to the item. Can someone explain what is going on in programming language terms and tell me how to fix this so that the last line would print 'B'?

Variable assignment in lisslist

This is a very basic question because I am a new user of SAGE with a background mostly in Java and C++. But in the following session: sage: var('A') A sage: var('B') B sage: for item in alist: item = B print item ....: B sage: for item in alist: print item ....: A I am not sure why the value of the item in the list is printed as 'A' instead of 'B'. During the previous loop it seems like it assigned the value 'B' and printed out that the item was 'B. This may be an issue of the line 'item = B' returning a transient copy of the item, instead of a reference to the item. Can someone explain what is going on in programming language terms and tell me how to fix this so that the last line would print 'B'?

click to hide/show revision 3
No.3 Revision

Variable assignment in list

This is a very basic question because I am a new user of SAGE with a background mostly in Java and C++. But in the following session: session:

sage: var('A')
A
sage: var('B')
B
sage: for item in alist:
    item = B
    print item
....:     
B
sage: for item in alist:
    print item
....:     
A

I am not sure why the value of the item in the list is printed as 'A' instead of 'B'. During the previous loop it seems like it assigned the value 'B' and printed out that the item was 'B. This may be an issue of the line 'item = B' returning a transient copy of the item, instead of a reference to the item. Can someone explain what is going on in programming language terms and tell me how to fix this so that the last line would print 'B'?

Variable assignment in list

This is a very basic question because I am a new user of SAGE with a background mostly in Java and C++. But in the following session:

sage: var('A')
A
sage: var('B')
B
sage: for item in alist:
    item = B
    print item
....:     
B
sage: for item in alist:
    print item
....:     
A

I am not sure why the value of the item in the list is printed as 'A' instead of 'B'. During the previous loop it seems like it assigned the value 'B' and printed out that the item was 'B. This may be an issue of the line 'item = B' returning a transient copy of the item, instead of a reference to the item. Can someone explain what is going on in programming language terms and tell me how to fix this so that the last line would print 'B'?

EDIT: I should explain that what I want to do is not just change the items in the list, so I don't want to just make alist conatin the variable 'B'. I actually want to assign the value 'B' to the variable 'A', so that when I type 'A' into SAGE, the output is 'B'. This can be accomplished in this example with the one command:

sage: A = B

but the point is that if I have two lists

  1. listVariables, e.g. [Y_1,...Y_n]
  2. listExpressions, e.g. [X2, X3, Z^*4, ..., XZ]

of unknown length and contents, except that the two lists have the same number of elements, I want to assign the value contained in listExpressions[i] to the variable in listVariables[i] by looping over the lists.

Variable assignment in list

This is a very basic question because I am a new user of SAGE with a background mostly in Java and C++. But in the following session:

sage: var('A')
A
sage: var('B')
B
sage: for item in alist:
    item = B
    print item
....:     
B
sage: for item in alist:
    print item
....:     
A

I am not sure why the value of the item in the list is printed as 'A' instead of 'B'. During the previous loop it seems like it assigned the value 'B' and printed out that the item was 'B. This may be an issue of the line 'item = B' returning a transient copy of the item, instead of a reference to the item. Can someone explain what is going on in programming language terms and tell me how to fix this so that the last line would print 'B'?

EDIT: I should explain that what I want to do is not just change the items in the list, so I don't want to just make alist conatin the variable 'B'. I actually want to assign the value 'B' to the variable 'A', so that when I type 'A' into SAGE, the output is 'B'. This can be accomplished in this example with the one command:

sage: A = B

but the point is that if I have two lists

  1. listVariables, e.g. [Y_1,...Y_n]
  2. listExpressions, e.g. [X2, X3, Z^*4, [X^2, X^3, Z^4, ..., XZ]XZ]

of unknown length and contents, except that the two lists have the same number of elements, I want to assign the value contained in listExpressions[i] to the variable in listVariables[i] by looping over the lists.