Duplicate edit : There is another question that has been marked as similar, but it is different because it talks only about .append and full list instanciation. My issue is about offset assignement (data[j]) and iterators (used in the first function).
I'm being a little confused with Python by-object information passing. From my text book, when the function receive it's parameters, it creates an alias to the initial object. Also, about mutable param : "we note that reassigning a new value to a formal parameter, such as by setting list = [ ], does not alter the actual parameter; such reassignment breaks the alias".
Now I have these 2 functions :
def mult(data, factor):
for val in data:
val *= factor
data = [i for i in range(5)]
mult(data, 3)
print(data)
def mult2(data, factor):
for j in range(len(data)):
data[j] *= factor
data = [i for i in range(5)]
mult2(data, 3)
print(data)
This yields the output 0,1,2,3,4 and 0,3,6,9,12 respectively.
In mult2, we change the value of data by reassigning a new value. Why isn't the alias broken?
Additionnaly, in the first function, if I loop through every value of the list and I change it, is the alias broken?