- In this video we're going to take a deeper look
at Passing Arguments to Functions.
Now, in most programming languages you have
what are known as Pass-by-value
and Pass-by-reference.
With Pass-by-value you pass a copy
of a value into a function
and because it's a copy the function can do
whatever it wants to the copy without having any effect
on the original value in Memory
wherever that may be.
Many languages also support Pass-by-reference
where the called function is told,
"This is where you can find the original data in Memory."
And therefore the called function
having access to the original data,
is able to potentially modify and manipulate
that data directly.
Now, in Python everything is Pass-by-reference
because everything is an object
and we don't directly manipulate the objects
we manipulate the references to the objects.
So when I pass a reference to an object
into a function, a copy of the reference is made
but not a copy of the object itself.
Which turns out to be really important
for performance because as you start getting into big data,
the objects that you're manipulating
may contain enormous amounts of information.
Which if we had to copy that data
into a function would take significant compute power.
So, in Python, just reiterating.
When you have a variable that refers to a value
the variable is some place in Memory
that just contains Addresses of objects
and the names by which we refer to those objects.
And then elsewhere in Memory are the objects themselves.
So this would represent a case where we had said
"assign to the variable X the value 7."
So now that we've introduced the concepts
of Pass-by-value and Pass-by-reference,
let's try to hammer this point home a little bit more
with an IPython session.
And let's start out with the variable X
initialized to 7.
Which creates the variable that we just showed you
in that preceding diagram.
Now, turns out that every object in Memory at a given time,
has a unique ID associated with it.
And you can access that information
via a function named ID.
So, for example, if I go ahead and say
"Give me the ID of whatever X refers to."
It's going to give me back a number
and by the way that number would probably be different
on your system and if you execute
a separate IPython sessions,
each IPython session would probably have
different numbers as well.
This number is known as the object's identity
and in many Python implementations
this is actually the Memory Address of the object
but that is not required,
so you certainly shouldn't rely on it
being the Memory Address of an object.
Now, to help me get through this a little bit easier
rather than typing a function definition,
I'm going to paste one in
for a function called "cube"
which is going to calculate the cube
of whatever number you give it.
And, what I wanna do is demonstrate
what the ID is of the variable that we receive
of the object that we receive
into the parameter called number.
So, the first thing we do is define this function,
it's going to print out that ID
then it's going to return the cube of number.
So let's define that, and let's go ahead
and call cube with the number 7.
And we can see that the ID is identical
to back in snippet to.
So the object that we're manipulating
inside the cube function is in fact
the object that X refers to,
that initially contains the value 7.
Now we didn't modify X, so X still contains the value 7
after the call to cube in this case.
However, it is possible that we can write functions
that modify objects as well.
Now, it turns out there's also another operator
that can help you understand this as well,
it's an operator called "is",
which you can use to test whether two objects
have the same identity.
So, let's go ahead and paste in another version
of the cube function here.
In this case, we're introducing the "is" operator
and what we're going to do is print out "number X is".
No, I'm sorry,
"number is x" and we're going to see a bulling value
true or false.
So, if number and x are the same object,
that is they have the same identity.
Then we'll get true, if they're different objects in Memory
we'll get false, as a result of that.
So if I define that and now once again
let's go call "cube"
and we'll give it "x" as an argument.
And we can see that "number" is indeed "x"
they are the same object in Memory.
Now it is possible to pass
both mutable and immutable objects as arguments
to functions in Python.
If you pass a mutable or modifiable object
you'll be able to change the original value in the color.
However, if you pass an immutable object
and there are quite a number of those in Python,
then any changes that appear to be made
to the original object are actually
creating new objects in Memory.
So to help with this point, let me grab another copy
of my "cube" function here and paste it in.
And so, here we've defined another version
of the "cube" function.
And in the block of that function
we're going to go ahead and display
the ID of whatever object we're receiving
by reference into the variable number.
Then we're going to try to modify that object,
now "number" is a local variable,
it's the parameter of the "cube" function.
And it's a copy of the reference
that was stored in the variable "x"
and passed into the "cube" function
as we're gonna make that call in just a moment
in the next snippet here.
But because it's a copy of the reference,
it's referring
it's still referring to the original
integer object in Memory
but integer objects are immutable.
So, anything that looks like it modifies an integer
actually creates a new integer object in Memory.
So with cube number stored
the new reference in number and then to prove to you
that is a different object,
we once again display numbers ID
then we return number to the caller.
So, let's now call "cube" to see what happens here
and we'll once again give it the variable "x".
And as you can see, as we come into the function
"x" has the ID shown here, which is the same
ID that was displayed up above
if we scroll back up, there's that same ID again.
And after we do the "cube" we have a new ID
of physically separate object in Memory.
And of course, you can go and check
the ID of "x" to see that it's still
referring to the same object that it started with,
and indeed it is the same ID once again.
So, when you are working
with objects in Python,
they are Pass-by-reference into the functions
and we were able to prove that by displaying
the IDs of the objects before and inside of the cause.
But, certain types of objects are immutable
and therefore the function is unable
to modify the original object in Memory.
So some of the immutable items in Python include
the numeric types integers and floating point numbers,
strings, tuples are immutable as well.
And there are a few other items
that we'll see in later lessons.