Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Immutable and Mutable objects in Python/Sagemath

Project: LS 30B-1 S18
Views: 266

Immutable and Mutable objects in python

Integers, floats, strings, booleans and tuples are immutable objects. list, dict and matrices (among other things) are mutable objects. In python, unlike many other programming languages (like C), assigning a variable to an object (ie var = 5) simply assigns the name var to represent that object instead of copying it. For immutable objects, there is usually no confusion.

a = 1 b = a # b is a reference to the immutable value 1 b = 5 # b is now a reference to the immutable value 5 print a, b, "no difference between a and b" a = "this is an immutable string" b = a b = "this is another immutable string" print "a = {}".format(a) print "b = {}".format(b)
1 5 no difference between a and b a = this is an immutable string b = this is another immutable string
# tuples are a list-like object that cannot be changed. A = (1,2,3,4) B = A # B is a reference to A; not a copy B[2] = 5 # tuples are immutable; you can't change them
Error in lines 3-3 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1044, in execute exec compile(block+'\n', '', 'single', flags=compile_flags) in namespace, locals File "", line 1, in <module> TypeError: 'tuple' object does not support item assignment

However, mutable objects like lists and matrices often just change the referenced object instead of copying. This can lead to some really unexpected behavior. Thus, assigning a list/matrix to a variable name just makes that name a reference to that list/matrix instead of actually copying the object.

To get around this, you should use the copy() function to make a copy of the object that will not reference the original.

A = [1,2,3,4] B = A # B is a reference to A; not a copy B[2] = 5 # since A and B are both references to the same list, changing the list pointed to by B also changes the list pointed to by A print "A = {}".format(A) print "B = {}".format(B) A.sort() print "A = {}".format(A) # A is sorted print "B = {}".format(B) # B is **also** sorted C = copy(A) C[0] = 99 print "A = {}".format(A) print "B = {}".format(B) print "C = {}".format(C) # C is changed without modifying A or B
A = [1, 2, 5, 4] B = [1, 2, 5, 4] A = [1, 2, 4, 5] B = [1, 2, 4, 5] A = [1, 2, 4, 5] B = [1, 2, 4, 5] C = [99, 2, 4, 5]
A = matrix([[1,2],[3,4]]) B = A # B is a reference to A; not a copy B[0,0] = 5 # since A and B are both references to the same matrix, changing the matrix pointed to by B also changes the matrix pointed to by A show( "A =" ,A) show( "B =", B)
A = (5234)\displaystyle \left(\begin{array}{rr} 5 & 2 \\ 3 & 4 \end{array}\right)
B = (5234)\displaystyle \left(\begin{array}{rr} 5 & 2 \\ 3 & 4 \end{array}\right)
C = copy(A) C[0,0] = 99 show( "A =" ,A) show( "B =", B) show( "C =", C) # C is changed without modifying A or B
A = (5234)\displaystyle \left(\begin{array}{rr} 5 & 2 \\ 3 & 4 \end{array}\right)
B = (5234)\displaystyle \left(\begin{array}{rr} 5 & 2 \\ 3 & 4 \end{array}\right)
C = (99234)\displaystyle \left(\begin{array}{rr} 99 & 2 \\ 3 & 4 \end{array}\right)