http://spreadsheets.google.com/ver?key=pCwvGVwSMxTzT6E2xNdo5fA&t=1229242768198000&pt=1229242748198000&diffWidget=true&s=AJVazbV8wTRdqv-uJ-exdnVEO7tqiCikJQ
answer below, quote here: ================================================= The following behavior is very confusing to me. I define a program def test(a): b=a b = a^2 return b Then if I assign x = 2 and then execute test(x) 4 I get x squared and x 2 Thus x is unchanged. However suppose now that I want the same on a single element of a matrix and define def testmatrix(a): b=a b[1,1] = a[1,1]^2 return b Then I set m = identity_matrix(2)*2 Thus: m [2 0] [0 2] and testmatrix(m) [2 0] [0 4] However now also m is changed ! m [2 0] [0 4] Outside the program ! ================================================= Well, m should be [2 0] [0 2] the same after the program execution. The program is solved changing the program to def testmatrix(a): b=a.copy() b[1,1] = a[1,1]^2 return b However, it is confusing to have a function with local variable a that returns another variable b locally equal to a and changes the value of a outside the function. ================================================= hi, i've read your bug report. Well, I can only tell you that you did the right things, observations and your solution with .copy() is also correct. The thing is, that python (and other languages like java) use references as their variables. References point to objects in memory. An object is a certain instance of a class in memory. In your integer number example, you are *changing* the reference when you square a (you create a new integer instance), whereas in your matrix example, the matrix stays the *same* - only something inside is changed. This has nothing to do with local variables, since the aren't really local in both cases. Also, if you want to have the reference behavior for integers as with the matrix, create a list/vector and pass the vector, manipulate the "0"th element and return the list/vector. If you have further questions about python (this is actually only about python) you can ask on the http://groups.google.com/group/sage-support list. harald --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
