Today I am going to explain the question that arouse in me while i was learning python. Is there something I can do to pass the variable by actual reference? Parameters are passed by value. The reason people are confused by the behaviour is twofold: the parameter passed in is actually a reference to a variable (but the reference is passed by value) some data types are mutable, but others aren't So, if you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object. If out pass an immutable object to a method, you still can't rebind the outer reference, and you can't even mutate the object. Okay, this is a little confusing. Let's have some examples. Let's try to modify the list that was passed to a method:
Since the parameter passed in is a reference to outer_list, not a copy of it, we can use the mutating list methods to change it and have the changes reflected in the outer scope. Now let's see what happens when we try to change the reference that was passed in as a parameter:
Since the the_list parameter was passed by value, assigning a new list to it had no effect that the code outside the method could see. The the_list was a copy of the outer_list reference, and we had the_list point to a new list, but there was no way to change where outer_list pointed. String - an immutable type It's immutable, so there's nothing we can do to change the contents of the string Now, let's try to change the reference
Again, since the the_string parameter was passed by value, assigning a new string to it had no effect that the code outside the method could see. The the_string was a copy of the outer_string reference, and we had the_string point to a new list, but there was no way to change where outer_string pointed. I hope this clears things up a little.
Written by Vivek Soundrapandi
Published Date: {{ article.pub_date }}
View all posts by: Vivek Soundrapandi