Until recently, I thought Python passed parameters by value. For example,
def method1(n):
n = 5
return n
n = 1
m = method1(n)
>>> n == 5
>>> False
But if I pass in a list to a method, like
def method2(list):
del list[0]
return list
list1 = [0,1,2,3,4,5]
list2 = method2(list1)
>>> list1
>>> [1,2,3,4,5]
it mutates the list. I did another test:
l1 = ['a','b','c','d']
x = method1(l1)
>>> l1
>>> ['a','b','c','d']
Here the list did not change. My question is
Why do these different cases happen?