0

In the following Python 2.7 code

a = 10
def foo(b):
    b = b * 2
    print a


foo(a)

variable 'a' and 'b' represents different variable spaces in memory so the result is always '10'.But in the following code

my_numbers = []

def func_while(numb):
    i = 0
    while i < 5:
        print "At the top i is %d" % i
        numb.append(i)

        i = i+1
        print "Number now: ", my_numbers

        print "At the bottom i is %d" % i


func_while(my_numbers)    

print "The numbers: "

for num in my_numbers:
    print num

Appending 'numb' variable (with the help of func_while() ) automatically makes changes in 'my_numbers' variable. How come ? are 'numb' variable and 'my_numbers' are different name of the same variable block ? IF yes then why its not true in the first code. Thanks

Mr.Mahajan
  • 307
  • 3
  • 10
  • Lists in python passes by reference – okuznetsov Jul 25 '16 at 13:55
  • 3
    Assignment doesn't mutate, `append` does. Try replacing `append` with `numb = numb + [i]` and see what happens. – Kevin Jul 25 '16 at 13:57
  • My main confusion is to clarify when we pass the argument in the function, does it creates new variable block ? or it creates a different name "numb" for the same variable block "my_number" ? – Mr.Mahajan Jul 25 '16 at 14:03

1 Answers1

0

As stated in the above comments, Python passes lists by reference. In fact, Python passes all reference objects by reference.

I think the answer to this post can do a better job further explaining this than I could.

EDIT: This explanation is somewhat incorrect. I have been shown that Python only has mutable or immutable objects. Therefore, they are not passed by reference/value. Regardless, the explanation, with this knowledge, is more or less the same.

Community
  • 1
  • 1
BrandonM
  • 390
  • 4
  • 12
  • Python passes all values the same, there's no difference between how lists (or other "reference objects", whatever those are) are passed, and other values like ints and strings. – Ned Batchelder Jul 27 '16 at 11:58
  • Untrue. Python pass standard datatypes as values, not references. Standard datatypes can include ints, floats, chars, etc. You can find some additional explanation (here) in the first answer.[http://stackoverflow.com/questions/15148496/python-passing-an-integer-by-reference] – BrandonM Jul 27 '16 at 18:15
  • Sorry, but this is something I know about, I've spoken about it at PyCon. All values are passed the same. But later mutations are possible on some values (like lists), and not others (like strings), which can lead to different effects. See this talk for details: http://nedbatchelder.com/text/names1.html. The SO answer you linked to is a good one: it also doesn't claim that different values are passed differently. – Ned Batchelder Jul 27 '16 at 23:06
  • Interesting and my apologies. Today I learned something. – BrandonM Jul 28 '16 at 13:50
  • 1
    No need for apologies, I'm glad it ended amicably, it doesn't always :) – Ned Batchelder Jul 28 '16 at 16:13