0

I am just started learning Python am getting bit confused after seeing the output of following program:

#!/usr/bin/python
# Function definition is here
def changeme( mylist ):
    "This changes a passed list into this function"
    mylist = [1,2,3,4]; # This would assig new reference in mylist
    print "Values inside the function: ", mylist
    return

# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );

print "Values outside the function: ", mylist

Values inside the function: [1, 2, 3, 4]
Values outside the function: [10, 20, 30]

Why is Values outside the function: [10, 20, 30], and not [1, 2, 3, 4] since we are passing the argument to the function by reference?

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199

3 Answers3

4

Works for me, with proper indentation:

def changeme(mylist):
    mylist.append([1, 2, 3, 4])
    print "Values inside the function: ", mylist

mylist = [10, 20, 30]
changeme(mylist)
print "Values outside the function: ", mylist

Output:

Values inside the function:  [10, 20, 30, [1, 2, 3, 4]]
Values outside the function:  [10, 20, 30, [1, 2, 3, 4]]

Note that altough semicolons are permitted at the end of the line, their use is discouraged because they serve no purpose and make the code look less clean. Also, you're probably looking for the extend method of mylist instead of append:

>>> mylist = [10, 20, 30]
>>> mylist.extend([1, 2, 3, 4])
>>> mylist
[10, 20, 30, 1, 2, 3, 4]

Re: update

In the updated changeme function you're discarding your reference to mylist, and replacing it with a reference to the new list you've just created. If you keep a reference to the old list, it will stay the same. This should illustrate the point:

def changeme(mylist):
    myref = mylist
    mylist = [1, 2, 3, 4]
    print "myref: ", myref
    print "mylist: ", mylist

Output:

myref:  [10, 20, 30]
mylist:  [1, 2, 3, 4]

To achieve the result you want, you use the python slice notation:

def changeme(mylist):
    mylist[:] = [1,2,3,4]
Community
  • 1
  • 1
Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
  • @Thanks Lazyr for your explaination but i would like to know if it is a case in C language what o/p you expect??is it behave the same way as its behaving here – Amit Singh Tomar May 31 '11 at 07:50
  • I'm sorry, I don't quite understand what you're asking. Could you try to rephrase? – Lauritz V. Thaulow May 31 '11 at 07:56
  • @Lazyr In C if we do pass array name as function argument then whatever operation we perform on that array ,reflects in original array ,which is not looks same in Python... – Amit Singh Tomar May 31 '11 at 08:10
  • @AMIT Python variables are more like C pointers than C variables. At the start of the function, `mylist` points to the list object `[10, 20, 30]` that was passed to the function. Then `mylist = [1, 2, 3, 4]` says `mylist` should point to a new list object `[1, 2, 3, 4]` instead. To achieve what you want you need to be careful to work on the object that is pointed to, that is, call its methods or assign to its attributes. You should also read up on mutable and non-mutable types in python, as they behave differently in these conditions. – Lauritz V. Thaulow May 31 '11 at 09:03
  • I'd suggest avoiding the name `mycopy` for something that is not actually a copy of anything. :-) – kindall Jun 13 '11 at 21:39
  • @kindall It's a copy of the reference to the list. :) But you're right, I'll fix it. – Lauritz V. Thaulow Jun 14 '11 at 08:11
3

By assigning a value, you are defining a new list, not updating the old one.

You can check by using the id() function on them.

Don
  • 16,928
  • 12
  • 63
  • 101
  • To update the passed list you need to use "destructive" operations on it, to change the list in place: .append, .extend, slice assignment. – ThomasH May 31 '11 at 08:00
1

mylist inside the function is a local variable. If you bind a new value to a local variable then you only have only changed the binding for the local variable. That won't affect any other variables that happen to be bound to the same object.

If you had mutated the object itself, e.g. by appending to the list, that would have been visible to anything using that same object.

Duncan
  • 92,073
  • 11
  • 122
  • 156