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?

2
  • 2
    I get [10, 20, 30, [1, 2, 3, 4]] for both. Commented May 31, 2011 at 7:28
  • I have updated the right code now!!which is giving me surprising o/p Commented May 31, 2011 at 7:34

3 Answers 3

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]
Sign up to request clarification or add additional context in comments.

6 Comments

@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
I'm sorry, I don't quite understand what you're asking. Could you try to rephrase?
@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 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.
I'd suggest avoiding the name mycopy for something that is not actually a copy of anything. :-)
|
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.

1 Comment

To update the passed list you need to use "destructive" operations on it, to change the list in place: .append, .extend, slice assignment.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.