-: Hi guys and welcome back.
In this video we're going to talk about
Mutable default parameters.
This is something that you really need
to know about in Python because if you
make this mistake it's gonna cost you a lot time
trying to find out what the problem is.
So let's get started.
Here we've got a student class and in the init
method we have two parameters.
The name which is a string and the grades
which is a list of integers
But it has a default value of empty list.
So by the way this is how you type hint
when you have both a type hint and a default value.
I'm not sure we even looked at it before
but nonetheless that is how it goes.
Though you've got your grades you can patch in
a list of grades if you want when you create the object
or you can use a default which is an empty list.
Then you save that and when you take an exam
your passing a result such as an integer.
we're going to put that in there actually
and then it appends to self dot grades.
"This is bad" I put a comment in there so you
don't do this.
Never make a parameter equal to a mutual value by default.
Here's why.
You created your student, Bob, and you used the
default grades, then you made him take an exam
which appends 90 to self dot grades
and finally you print, Bob's, grades.
So let's save that and run it and you see you
get 90 out so yeah, no problem.
But then what if you create one more student
such as, Rolf, and you don't make him take any exams.
What are we going to see now?
Well let me run this code
and wallah both student's
have 90.
Even though only Bob took an exam.
This is why mutability is so important
in Python and why this is a bad idea.
The function parameters these default ones
evaluate when the function is defined,
not when the function is called.
When the class is created this function is
evaluated so that Python knows what the
parameters are and what the name is
and so on and this one too.
And there is default value is created.
That means that if you use the default
self dot grades is a name for this specific list
And when you create two students.
Self dot grades in both of them are names
to the same list.
This one that was created when the function was created.
Not when the function was called
So as long as your using the default parameters
for all your students they will share grades.
That's probably not what you want.
Rather do note that if you pass in
your own list here then these will be different
because now Bob is using this list
And Rolf is using this one.
If you then define a third one that uses this one
those two will share grades
but this one will be unaffected
because that's using his own list.
So avoid this problem by not using Mutable default
values for parameters.
Instead what you can do is you can make it
equal to none and then you can make this equal
to grades
or empty list
and what happens here is
that if this is none then none or empty list
will evaluate to empty list.
Now instead of list of integers you may want to do
import optional as well and make this an optional
list in integers.
That's just a slightly better way of handling this here
so Python knows that it is optional
and it may not be a list initially it maybe none
initially but your gonna set it to a list later on.
So this is a better way of handling that type of
hint as well.
So now this will work the way we intended to.
By pressing play you see that Bob has 90
and Rolf has nothing.
It is very important my word of advise is to
use default parameter values that are mutable.
Just stick to immutable ones like
integers, strings, floats, bullion, tuples
things like that and if not use this pattern here
to define them inside the function and not
in the parameter list.
Thank you for joining me in this video,
I hope it's been useful and I'll see you in the next one.