def nochange(y): y = 1 x = 2 nochange(x) print x # prints 2 def nochange(y): y = ['hello'] x = ['not hello'] nochange(x) print x # prints ['not hello'] # What is happening in the above is the following, # # You are creating a list which has the content ['not hello'] and then telling # the varible 'x' to point at this list. # # When you call nochange, the y argument is set to point to what x is also # pointing to. The next line then tells y to point somewhere else! # So why does the following work? def changeme(y): y.append('hello') x = [] changeme(x) print x # prints ['hello'] # You created an empty list and tell the varible 'x' to point to this list. # You then call the changeme function, the y argument is set to point to what x # is also pointing to. Then you say "call the append function on this thing # which y points to". # # The append function then changes the contents of the object that y and x are # both pointing too. # So why doesn't the following work def changeme(y): y += 1 x = 2 changeme(x) print x # prints 2 # The reason this does not work is because an integer is immutable. You can not # change an int object. # # This means that y += 1 is mapped to y = y+1 # # Which means, take the object at y, add one and assign y the resulting object. #