I was currently doing the introductory course of python provided by mit on edx. And, the professor is currently teaching the use of functions as objects. I am having confusion in one of the codes where he is defining a function and taking two arguments- one is a list and other is some other function.
def applyToEach(l,f):
for i in range(len(l)):
l[i]=f(l[i])
l=[1,-2,3.4]
applyToEach(l,abs)
print(l)
My question is: the changes/modifications in the list is taking place inside the function. So, how they are getting reflected outside the function? According to me, there should not be any changes in the list when I print it because I made all the changes inside the function. I mean that different environments are created for different functions and it worked this way only in the previous exercises and therefore changes are not reflected in our main environment. Correct me wherever I am wrong, because when I am running this code, the modified list is getting printed. So, where I am wrong in the given logic?