Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 question from reading python functions tutorial
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
subconscious




PostPosted: Sun Mar 18, 2012 7:57 pm   Post subject: question from reading python functions tutorial

def changeme( mylist ):
"This changes a passed list into this function"
mylist.append([1,2,3,4]);
print "Values inside the function: ", mylist
return
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist

When i run the program, here is the result.

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

Another program is:
def changeme( mylist ):
"This changes a passed list into this function"
mylist = [1,2,3,4]
print "Values inside the function: ", mylist
return
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist

Here is the result:
Values inside the function: [1, 2, 3, 4]
Values outside the function: [10, 20, 30]
Why are the second program's "Values outside the function" result is " [10, 20, 30]" rather than "[1, 2, 3, 4]"
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Sun Mar 18, 2012 9:55 pm   Post subject: Re: question from reading python functions tutorial

I'll try to make this understandable, sorry if it's not.

Python passes arguments to its functions using "pass by reference". This means that when you call a function with an argument, you are telling it where the value of it's argument is in memory. In fact, I'm pretty sure (not 100%) that all variables are actually just the memory location of their values. So to pass a variable to a function is to pass a the location (like an address) of a block of memory where some value is stored.

Here's your first function:
code:
def changeme( mylist ):
    mylist.append([1,2,3,4]);
    print "Values inside the function: ", mylist
    return


So now we give it the memory location of a list [10, 20, 30].
Because arguments are passed using their memory location, mylist.append([1, 2, 3, 4]) acts on the list in the memory location that was given to the function chageme and adds [1, 2, 3, 4] to it. So the list that you gave to the function is now [10, 20, 30, [1, 2, 3, 4]]. At this point we print this list and return.

Here's your second function:
code:
def changeme( mylist ):
    mylist = [1,2,3,4]
    print "Values inside the function: ", mylist
    return


Now we give this function the memory location of a list [10, 20, 30].
What does mylist = [1, 2, 3, 4] do?
What it actually does is declare mylist to now be the location in memory of a different list. So now we have mylist that is the location in memory of a list [1, 2, 3, 4]. We print the list and return.

Now we have returned to he main program. You have to realise that the mylist inside the function and the mylist outside the function are two different variables. We could have just as well written
code:
def changeme( functionList ):
    functionList .append([1, 2, 3, 4]) OR functionList = [1,2,3,4]
    print "Values inside the function: ", functionList
    return
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist


This will give the same output as your two functions. The name used for the argument inside the function can be anything.
In this light, we examine the two programs after they have returned from the function. Recall that mylist, being a variable is just the location in memory where we are storing a list.

In the first program, the list at the memory location of mylist was changed to [10, 20, 30, [1, 2, 3, 4]] by the function. So we print it's value which is the same thing that we printed in the function.
In the second program, we changed mylist to a different memory location in the function. But remember the name of the list in the function doesn't matter. So if we consider the version of the program that I wrote, we chaged functionList to a different memory location. So when we return from the function, mylist is still the location in memory of a list [10, 20, 30] which we print.

The key is to realise, that changing the value (a memory location) of mylist in the function does not affect the value (another memory location) of the mylist outside the function, but the function can change what is at the memory location of mylist.

Hope this helped.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: