Pass-By-Value vs Pass-By-Reference in Python
00:00 Because Python handles variables in a different way than languages like C++, Java, and Pascal do, we should expect the notions of pass-by-value and pass-by-reference to be a little bit different than from those other languages.
There’s nothing specific about a particular memory location, wherever that may be. When I then reassign
10, a new object is created having nothing to do with the memory where the first object was made, and
x is simply made to refer to that new object.
This is the same program that we had in C++. We define a function
f() to print out the value of its parameter, and here we’re going to do something a little bit different. To actually show the creation and manipulation of objects, we’re going to display the identifier, the unique number given to an object when it’s being used.
So, the output statement’s going to be a little bit different than it was in the C++ version, but the process is still going to be the same. In the function, we take a parameter, print out its value, change the value of that parameter, and print information about its new value and new object. In the main part of our program, we’re going to assign
x to be
5, print out its value and its object identifier, call the function, and then print the value of
x again to see what, if any, effect the function had on the variable
We then called the function and passed that object as the parameter. We see in the first output statement,
fx had a value of
5, and its identifier—the same number referring to the same object that
But all of this is happening within the scope of the function
f(). Scope refers to when an identifier has meaning, and once this function quits execution, anything related to that function is gone.
So this change of
fx only happened within the scope of the function
f() is finished, all we have left is
x still has the value of
5 and is still referring to the same object that it was in the beginning.
03:59 Whatever object is provided, that’s what the parameter refers to. Assignment also binds the name of a variable to an object. When a variable is bound to an immutable object, the object can’t be changed.
04:14 The variable can be bound to a new object, but we are not changing the contents of the object, we are replacing it with a new object. Other terms you might see for this are pass-by-object, pass-by-object-reference, or pass-by-sharing. A Python function can’t change the value of an argument by reassigning the corresponding parameter to something else. And pay attention to that word “reassigning,” because we will come back to that in the next lesson.
x refers to a new object after being given whatever object
i represents each time it’s called, it’s not changing the object that
i itself is referring to. So we can see the first time through
i is still
06:48 You saw that you can’t change the value of an argument by reassigning its correspond parameter to something new. However, if the object is a mutable object, then the function can make changes to that object that it receives—not replacing it with a new object, but changing the object it received as an argument, and those changes will be reflected in the object that was passed to it when the function finishes its execution.
Become a Member to join the conversation.