1
1

00:00:01,170  -->  00:00:02,630
<v ->Hi guys and welcome back.</v>
2

2

00:00:02,630  -->  00:00:04,210
In this video we're going to talk about
3

3

00:00:04,210  -->  00:00:06,312
Mutable default parameters.
4

4

00:00:06,312  -->  00:00:08,410
This is something that you really need
5

5

00:00:08,410  -->  00:00:09,770
to know about in Python because if you
6

6

00:00:09,770  -->  00:00:12,386
make this mistake it's gonna cost you a lot time
7

7

00:00:12,386  -->  00:00:14,600
trying to find out what the problem is.
8

8

00:00:14,600  -->  00:00:16,590
So let's get started.
9

9

00:00:16,590  -->  00:00:19,101
Here we've got a student class and in the init
10

10

00:00:19,101  -->  00:00:21,070
method we have two parameters.
11

11

00:00:21,070  -->  00:00:23,659
The name which is a string and the grades
12

12

00:00:23,659  -->  00:00:26,490
which is a list of integers
13

13

00:00:26,490  -->  00:00:30,093
But it has a default value of empty list.
14

14

00:00:31,460  -->  00:00:33,210
So by the way this is how you type hint
15

15

00:00:33,210  -->  00:00:35,640
when you have both a type hint and a default value.
16

16

00:00:35,640  -->  00:00:37,290
I'm not sure we even looked at it before
17

17

00:00:37,290  -->  00:00:39,820
but nonetheless that is how it goes.
18

18

00:00:39,820  -->  00:00:42,520
Though you've got your grades you can patch in
19

19

00:00:42,520  -->  00:00:45,860
a list of grades if you want when you create the object
20

20

00:00:45,860  -->  00:00:49,420
or you can use a default which is an empty list.
21

21

00:00:49,420  -->  00:00:52,770
Then you save that and when you take an exam
22

22

00:00:52,770  -->  00:00:55,160
your passing a result such as an integer.
23

23

00:00:55,160  -->  00:00:57,000
we're going to put that in there actually
24

24

00:00:57,000  -->  00:01:00,497
and then it appends to self dot grades.
25

25

00:01:00,497  -->  00:01:02,800
"This is bad" I put a comment in there so you
26

26

00:01:02,800  -->  00:01:03,730
don't do this.
27

27

00:01:03,730  -->  00:01:08,730
Never make a parameter equal to a mutual value by default.
28

28

00:01:08,900  -->  00:01:09,930
Here's why.
29

29

00:01:09,930  -->  00:01:12,680
You created your student, Bob, and you used the
30

30

00:01:12,680  -->  00:01:16,113
default grades, then you made him take an exam
31

31

00:01:16,113  -->  00:01:18,970
which appends 90 to self dot grades
32

32

00:01:18,970  -->  00:01:20,510
and finally you print, Bob's, grades.
33

33

00:01:20,510  -->  00:01:23,300
So let's save that and run it and you see you
34

34

00:01:23,300  -->  00:01:26,670
get 90 out so yeah, no problem.
35

35

00:01:26,670  -->  00:01:28,960
But then what if you create one more student
36

36

00:01:30,550  -->  00:01:35,293
such as, Rolf, and you don't make him take any exams.
37

37

00:01:36,590  -->  00:01:38,040
What are we going to see now?
38

38

00:01:40,040  -->  00:01:41,290
Well let me run this code
39

39

00:01:42,430  -->  00:01:45,280
and wallah both student's
40

40

00:01:45,280  -->  00:01:46,940
have 90.
41

41

00:01:46,940  -->  00:01:50,370
Even though only Bob took an exam.
42

42

00:01:50,370  -->  00:01:52,890
This is why mutability is so important
43

43

00:01:52,890  -->  00:01:55,500
in Python and why this is a bad idea.
44

44

00:01:55,500  -->  00:01:59,380
The function parameters these default ones
45

45

00:01:59,380  -->  00:02:02,030
evaluate when the function is defined,
46

46

00:02:02,030  -->  00:02:05,050
not when the function is called.
47

47

00:02:05,050  -->  00:02:08,160
When the class is created this function is
48

48

00:02:08,160  -->  00:02:10,350
evaluated so that Python knows what the
49

49

00:02:10,350  -->  00:02:11,820
parameters are and what the name is
50

50

00:02:11,820  -->  00:02:14,550
and so on and this one too.
51

51

00:02:14,550  -->  00:02:18,303
And there is default value is created.
52

52

00:02:19,540  -->  00:02:22,430
That means that if you use the default
53

53

00:02:22,430  -->  00:02:27,400
self dot grades is a name for this specific list
54

54

00:02:27,400  -->  00:02:29,350
And when you create two students.
55

55

00:02:29,350  -->  00:02:31,875
Self dot grades in both of them are names
56

56

00:02:31,875  -->  00:02:33,950
to the same list.
57

57

00:02:33,950  -->  00:02:37,070
This one that was created when the function was created.
58

58

00:02:37,070  -->  00:02:38,670
Not when the function was called
59

59

00:02:39,600  -->  00:02:42,474
So as long as your using the default parameters
60

60

00:02:42,474  -->  00:02:46,370
for all your students they will share grades.
61

61

00:02:46,370  -->  00:02:48,840
That's probably not what you want.
62

62

00:02:48,840  -->  00:02:50,454
Rather do note that if you pass in
63

63

00:02:50,454  -->  00:02:53,726
your own list here then these will be different
64

64

00:02:53,726  -->  00:02:56,777
because now Bob is using this list
65

65

00:02:56,777  -->  00:02:59,260
And Rolf is using this one.
66

66

00:02:59,260  -->  00:03:01,520
If you then define a third one that uses this one
67

67

00:03:01,520  -->  00:03:02,970
those two will share grades
68

68

00:03:02,970  -->  00:03:04,410
but this one will be unaffected
69

69

00:03:04,410  -->  00:03:06,343
because that's using his own list.
70

70

00:03:07,340  -->  00:03:11,360
So avoid this problem by not using Mutable default
71

71

00:03:11,360  -->  00:03:12,710
values for parameters.
72

72

00:03:12,710  -->  00:03:14,580
Instead what you can do is you can make it
73

73

00:03:14,580  -->  00:03:18,360
equal to none and then you can make this equal
74

74

00:03:18,360  -->  00:03:19,840
to grades
75

75

00:03:19,840  -->  00:03:21,810
or empty list
76

76

00:03:21,810  -->  00:03:23,080
and what happens here is
77

77

00:03:23,080  -->  00:03:27,340
that if this is none then none or empty list
78

78

00:03:27,340  -->  00:03:29,710
will evaluate to empty list.
79

79

00:03:29,710  -->  00:03:32,223
Now instead of list of integers you may want to do
80

80

00:03:32,223  -->  00:03:35,990
import optional as well and make this an optional
81

81

00:03:38,250  -->  00:03:39,240
list in integers.
82

82

00:03:39,240  -->  00:03:43,000
That's just a slightly better way of handling this here
83

83

00:03:43,000  -->  00:03:44,790
so Python knows that it is optional
84

84

00:03:44,790  -->  00:03:47,090
and it may not be a list initially it maybe none
85

85

00:03:47,090  -->  00:03:50,170
initially but your gonna set it to a list later on.
86

86

00:03:50,170  -->  00:03:52,730
So this is a better way of handling that type of
87

87

00:03:52,730  -->  00:03:53,563
hint as well.
88

88

00:03:54,420  -->  00:03:57,150
So now this will work the way we intended to.
89

89

00:03:57,150  -->  00:04:00,350
By pressing play you see that Bob has 90
90

90

00:04:00,350  -->  00:04:02,390
and Rolf has nothing.
91

91

00:04:02,390  -->  00:04:04,806
It is very important my word of advise is to
92

92

00:04:04,806  -->  00:04:08,590
use default parameter values that are mutable.
93

93

00:04:08,590  -->  00:04:11,130
Just stick to immutable ones like
94

94

00:04:11,130  -->  00:04:13,357
integers, strings, floats, bullion, tuples
95

95

00:04:13,357  -->  00:04:17,120
things like that and if not use this pattern here
96

96

00:04:17,120  -->  00:04:19,608
to define them inside the function and not
97

97

00:04:19,608  -->  00:04:21,323
in the parameter list.
98

98

00:04:22,890  -->  00:04:24,260
Thank you for joining me in this video,
99

99

00:04:24,260  -->  00:04:27,110
I hope it's been useful and I'll see you in the next one.
