0

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?

thisisashwani
  • 1,748
  • 2
  • 18
  • 25

2 Answers2

0

Because they are the same objects. You can verify that through id() or is.

#!/usr/bin/env python
#-*- coding:utf-8 -*-

def applyToEach(l,f):
    print 'inside id is: ', id(l)
    for i in range(len(l)):
        l[i]=f(l[i])

l=[1,-2,3.4]
print 'outside id is: ', id(l)

applyToEach(l,abs)
print(l)

In Python, passing a value to a function equals 'para = value'. However, '=' only add a reference to the value but not a copy.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Stephen Lin
  • 4,852
  • 1
  • 13
  • 26
  • great!! your explanation using id() removed my doubt that what will happen when i send some integer x as a parameter, because when i checked it, the id of some integer say x inside the function,that i am passing as a parameter was same until i did some modifications in it. After that the id of x changed inside the function and it no more matched with the id of x outside the function. – thisisashwani Oct 22 '14 at 01:12
  • @dex Yes, id is the unique remark of a variable in Python. If this answer helps, please accept it. :) – Stephen Lin Oct 22 '14 at 02:23
0

The list is being passed as an argument to the function. When a list is created in python a memory location is allocated to it for reference. Now, when you are passing the list (or even do any operation on the list), the assigned memory location is passed. So, whatever modifications are done, that is reflected in the list itself. But the memory location remains the same. So, regardless of whether you access it from outside of the function or inside the function it points to the same memory location. And hence, the changes done, inside are visible outside the function.

Let the list be

Fruit = ['Mango', 'Apple', 'Orange']

While we assign the list to value Fruit, a memory location is assigned to it (here 4886) so id(Fruit) = 4886 Now, suppose the list 'Fruit' is being passed to a function, inside which it is being modified as follows :-

def fun(li=[]):
    li[1] = 'Grape'
    li.extend(['Apple', 'Banana'])

So, what happens behind the scene is, while the list is passed to the method as argument, actually, the reference (or address) of the list is passed. So, even if you access the list and modify it within the function, it modifies the original list as it refers to the list reference. So, the list reference get updated accordingly.

After, the modification also, it will show the list updated as inside the method. But, the memory location will be the same i.e. id(Fruit) = 4886

Even if you assign the list to a new list i.e. temp = Fruit, same will be the scenario. temp will refer to the same location as of Fruit, both the list will point to the same location.

To avoid this situation, what you have to do is :-

temp = list(Fruit)

which will create a new list with a new memory location and hence the id(Fruit) will differ from id(temp).

Following link will give you a better understanding on how list works in python :- http://www.python-course.eu/deep_copy.php

Pabitra Pati
  • 457
  • 4
  • 12
  • your explanation with id() made it quite clear, as i implemented it in my program and the link further cleared the concept. – thisisashwani Oct 22 '14 at 01:29