Python: Mutable, Immutable… everything is object!

  • what are id and type functions
  • what is the difference between mutable objects and immutable objects
  • why does it matter and how differently does Python treat mutable and immutable objects
  • how arguments are passed to functions and what does that imply for mutable and immutable objects


ID and types

a = 5
a = (1, 2)
b = [1, 3, 5]
c = "Maroua"
tel = {'jack': 4098, 'sape': 4139}


<class 'tuple'>
<class 'list'>
<class 'str'>
<class 'dict'>

mutable objects

  • Lists
  • Sets
  • Dictionaries


my_list = [1,2,3,4,5,6]print(my_list)print(id(my_list))>>> [1,2,3,4,5,6]>>> 1863010210504my_list.append(7)print(my_list)print(id(my_list))>>> [1,2,3,4,5,6,7]>>> 1863010210504


lst = ['a', 'b', 'c', 'd']
my_set = set(lst)
# Adding element to the set
{'c', 'b', 'd', 'a'}140114243505952{'c', 'd', 'f', 'a', 'b'}140114243505952
 Number_pieces = 25
Number_pieces = 30
<class 'int'>
<class 'int'>
tuple_1 = (1, [3, 5], "school")
print("tuple before {}".format(tuple_1))
tuple_1[1][1] = 6
print("tuple after {}".format(Tuple_1))
tuple before (1, [3, 5], "school")
tuple before (1, [3, 6], "school")
def foo1(a):
# function block
a += 1
print('id of a:', id(a)) # id of y and a are same
return a

# main or caller block
x = 10
y = foo1(x)

# value of x is unchanged
print('x:', x)

# value of y is the return value of the function foo1
# after adding 1 to argument 'a' which is actual variable 'x'
print('y:', y)

print('id of x:', id(x)) # id of x
print('id of y:', id(y)) # id of y, different from x


id of a: 1456621360
x: 10
y: 11
id of x: 1456621344
id of y: 1456621360
  • Original object integer x is immutable (unchangeable). A new local duplicate copy a of the integer object x is created and used inside the function foo1() because integers are immutable objects and can’t be changed in placed. The caller main block where variable x is created has no effect on the value of the variable x.
  • The value of variable y is the value of variable a returned from function foo1() after adding 1.
  • Variable x and y are different as you can see their id values are different.
  • Variable y and a are same as you can see their id values are same. Both point to same integer object.
def foo2(func_list):
# function block
func_list.append(30) # append an element
print('original list:', list1)
print('list1 id:', id(list1))

print('value of list2:', list2)
print('list2 id:', id(list2))

print('\nafter foo2():', list1)
print('list1 id:', id(list1))

print('value of list2:', list2)
print('list2 id:', id(list2))
original list: [10, 20]
list1 id: 24710360
value of list2: [10, 20]
list2 id: 24710360
after foo2(): [10, 20, 30]
list1 id: 24710360
value of list2: [10, 20, 30]
list2 id: 24710360
  • We have created a list object list1 and assigned same object to a new variable list2. Now both list1 and list2 points to the same memory where actual list object [10, 20] is stored.
  • We passed the value list1 variable into the function argument func_list. We appended, deleted and modified the list1 object element in function foo2() through argument func_list.
  • As you have noticed that actual object list1 is changed in the main block when we changed its value in the function block.
  • You should also notice that the value of list2 variable also changes when the value of list2 changes. As we have also read, this is because both list1 and list2 variable points to same list object [10, 20].
  • list1 object ID doesn’t change after every call to function foo2().This is because list1 is mutable and can be modified. Therefore, changing list1 object modifies original object value and doesn’t create the new object.

Thanks for reading!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store