Welcome to Engineering Python.
This is a Python programming course for engineers.
Memory management is a complex concept in Python.
However, it is very important to understand the basics of memory management because it
will help us understand why Python operations behave the way they do.
In this video, I'll try to use very simple terms to explain memory management and its
application in modifying function parameters in-place.
Python memory management is quite different from other programming languages.
Also, the memory management for immutable objects and the memory management for mutable
objects are different.
We will talk about how it's done for immutable objects first.
Python immutable objects include integers, floats, strings, and tuples.
They are simple objects and store their own values.
Before I show some examples, let's reset the environment and clear the memory using the
reset magic.
Proceed?
Yes.
This will delete all the variables defined in the Python programming environment.
It provides a clean workspace for us.
After we are done with the resetting, we assign this integer 10 to variable x.
The Python built-in id function returns the identity of x.
This is the address of variable x in the memory.
Your output may be different from mine because we are using different computers.
What actually happened is that we built a link between variable x and the number 10,
which is stored in this location in the memory.
Let's assign 10 to y as well.
And show the id of y.
It turns out, y is pointing to the same 10 already in the memory because the ids are
the same.
We can confirm this by checking if x is y.
The answer is True.
x and y are both pointing to the same 10.
Next, we completely change y by assigning a string hello to it.
y's id is changed.
x is not y anymore.
In the memory, x and y are pointing to two different objects.
Next, we update x by adding 1 to itself.
x's value is changed from 10 to 11.
Its id is also changed.
What happened in the memory is that we broke the link between x and 10, and linked x to
another memory location that holds the number 11.
Python mutable objects are also called container objects.
They include lists, dictionaries, sets, and some user-defined types.
They work as containers and store the references to simple immutable objects.
They may also store the references to other mutable objects.
Again, let's reset the workspace using the reset magic before we proceed to the examples.
We assign this list of two elements to x.
In the memory, x is pointing to a container.
The address of the container is x's id.
This container further points to these two numbers, 10 and 20, at different locations
in the memory.
We assign this hello string to y.
Because y is immutable, it is a simple object.
It directly points to hello in the memory.
This is the id of y.
Next, we assign x to y. y's id is the same as x's id now.
In the memory, the link between y and hello is broken, and y is now pointing to this container,
just like x.
At this moment, we change the first element of y from 20 to 25.
Remember, Python indexes start with 0.
In the memory, this last reference is changed and now pointing to this integer number 25
at a different location of the memory.
As a result, x's value is also changed to this new list, 10 and 25.
Next, we append another number, 30, to the end of y.
This is what happened in the memory.
As a result, x is also changed to the new list with three elements.
To demonstrate more examples, let's reset and clear the memory again.
Assign this list to x.
This is what happened in the memory.
Next, assign this list to y as well.
We find x's id and y's id are different.
Is x the same as y?
This time, the answer is False.
In the memory, x and y are pointing to two different containers, although both containers
point to the same elements, 10 and 20.
We can check this by showing x zero's id and x one's id.
Compare them with y zero's id and y one's id.
They are exactly the same.
In this example, y is called a copy of x, and vice versa.
Because Python functions pass parameters by assignment, the above memory management rules
will also apply to functions.
Therefore, if the parameter is a mutable object, in-place changes to the object inside a function
will be visible to the outside world.
In addition to using the return statement, modifying function parameters in-place is
another way of communicating back to the caller.
This image summarizes what will happen in different scenarios.
The question we are interested in can be described like this.
x is assigned to y.
When y changes, will x also change?
The answer is it depends.
If x is immutable, then when y changes, x doesn't change.
If x is mutable, when something else is assigned to y in whole, like this one, y is completely
replaced by a string, then x doesn't change.
However, when y is modified in-place, like this one, we append one number to y in-place,
then this change will also affect x and x changes too.
Let's see three examples.
Note that none of the following examples has a return statement.
This is the first example.
This update function will update y's value by adding 5 to itself.
Outside of the function, we have a variable x with a value 10.
We pass x as the argument with an intention to change its value to 15.
However, because x is an integer number, which is immutable, this will not work.
x is still 10.
This is the second example.
This update function will update y to this list.
That means, something else is assigned to y in whole.
Outside of the function, we have a variable x, which is a list and mutable.
We pass x as the argument with an intention to change it to this new list.
However, because this assignment is not done in-place, it will not work.
x is still the old list.
This is the last example.
x is a list, which is mutable.
We pass it as the argument with an intention to increase each element by 5.
In this update function, we use a for loop to increase each element by 5.
This is an element-wise in-place modification.
So, after we modify y, x will also change.
You can see the printed x is 15 and 25 now.
Okay, that was about memory management and modifying function parameters in-place.
The course materials are available on YouTube and GitHub.
You can watch the course videos in sequence.
If you like this video, please subscribe and share.
I'm Yong Wang.
Thanks for watching.