WEBVTT

1
00:00:01.020 --> 00:00:02.550
<v ->Hi guys and welcome back.</v>

2
00:00:02.550 --> 00:00:03.670
In this video and the next,

3
00:00:03.670 --> 00:00:06.860
I wanted to talk with you about Mutability in Python

4
00:00:06.860 --> 00:00:09.000
and how it can affect your programmes.

5
00:00:09.000 --> 00:00:11.710
It's a very important part of Python to know about,

6
00:00:11.710 --> 00:00:12.860
so let's dive right in.

7
00:00:14.230 --> 00:00:18.370
Here we've got a list, and we have given it the name a.

8
00:00:18.370 --> 00:00:20.510
Then we have created the name b

9
00:00:20.510 --> 00:00:24.000
and made it equal to the value of name a.

10
00:00:24.000 --> 00:00:26.840
This way of saying things that I'm saying right now

11
00:00:26.840 --> 00:00:29.870
is very important because you'll see quite often,

12
00:00:29.870 --> 00:00:32.897
people say, "You've created a list called a,

13
00:00:32.897 --> 00:00:34.507
"and then you created a list called b.

14
00:00:34.507 --> 00:00:37.080
"That's not correct in Python."

15
00:00:37.080 --> 00:00:39.510
a and b are names.

16
00:00:39.510 --> 00:00:42.490
The value is this list.

17
00:00:42.490 --> 00:00:46.570
So if you print the ID of a,

18
00:00:46.570 --> 00:00:49.660
and then you print the ID of b,

19
00:00:49.660 --> 00:00:52.190
and in CPython, the Python version that I'm using

20
00:00:52.190 --> 00:00:53.760
and that most of you will be using,

21
00:00:53.760 --> 00:00:57.350
the ID function, when you give it any object

22
00:00:57.350 --> 00:00:58.510
such as this list,

23
00:00:58.510 --> 00:01:02.740
will give you back the location in memory of them,

24
00:01:02.740 --> 00:01:04.110
so by running this code here,

25
00:01:04.110 --> 00:01:07.300
you can see that the output is exactly the same.

26
00:01:07.300 --> 00:01:11.930
That means that a and b are names for the same object,

27
00:01:11.930 --> 00:01:12.823
the same thing.

28
00:01:14.020 --> 00:01:16.600
Indeed you can see that being the case

29
00:01:16.600 --> 00:01:21.600
if you just do a.append 35,

30
00:01:21.920 --> 00:01:24.723
and then you print a and print b.

31
00:01:26.000 --> 00:01:29.050
If you do that, you'll see that both a and b change

32
00:01:29.050 --> 00:01:30.590
when you modified a.

33
00:01:30.590 --> 00:01:34.893
You appended 35 to a, and both a and b ended up with 35.

34
00:01:36.140 --> 00:01:39.010
That means that both a and b are references

35
00:01:39.010 --> 00:01:42.960
to the same object, this one list.

36
00:01:42.960 --> 00:01:46.330
However, if you did b equals another list,

37
00:01:46.330 --> 00:01:48.259
then this would be different now.

38
00:01:48.259 --> 00:01:51.080
You have one list that has a 35 in it,

39
00:01:51.080 --> 00:01:53.120
and the other one that does not.

40
00:01:53.120 --> 00:01:56.050
In Python, this is the syntax for creating an empty list,

41
00:01:56.050 --> 00:01:58.560
so both of these are created separately,

42
00:01:58.560 --> 00:02:00.370
and they are stored separately.

43
00:02:00.370 --> 00:02:02.540
Once again, going to the ID,

44
00:02:02.540 --> 00:02:06.510
you'll see that these are indeed now different numbers.

45
00:02:06.510 --> 00:02:09.400
So let me lay that, and you can see that you get

46
00:02:09.400 --> 00:02:10.590
two different numbers.

47
00:02:10.590 --> 00:02:12.830
You can compare the last couple digits there.

48
00:02:12.830 --> 00:02:15.650
So the fact that you can change a list

49
00:02:15.650 --> 00:02:17.820
after you have created it means

50
00:02:17.820 --> 00:02:19.770
that a list is mutable.

51
00:02:19.770 --> 00:02:23.160
You can mutate it; that's the same thing as changing.

52
00:02:23.160 --> 00:02:27.830
Some values can't be changed, and they are immutable values.

53
00:02:27.830 --> 00:02:30.750
In Python, all things are mutable

54
00:02:30.750 --> 00:02:35.750
because everything's an object unless there are specifically

55
00:02:35.760 --> 00:02:40.143
no ways of changing the properties of the object itself.

56
00:02:40.980 --> 00:02:43.160
Let's go into an example.

57
00:02:43.160 --> 00:02:45.463
For example, if you define these to be tuples,

58
00:02:46.840 --> 00:02:50.230
there are actually no ways of adding or removing

59
00:02:50.230 --> 00:02:51.770
an element from a tuple.

60
00:02:51.770 --> 00:02:53.740
If you do a.append, you're gonna get an error

61
00:02:53.740 --> 00:02:56.710
saying that tuples don't have an append method in them.

62
00:02:56.710 --> 00:03:01.080
If you do a equal a plus something,

63
00:03:01.080 --> 00:03:03.630
you're going to create a new tuple.

64
00:03:03.630 --> 00:03:05.890
You're not going to be modifying that one.

65
00:03:05.890 --> 00:03:08.070
So tuples are immutable.

66
00:03:08.070 --> 00:03:10.470
Let's take a look at another example.

67
00:03:10.470 --> 00:03:15.190
Imagine you have two numbers such as 8597 and 8597.

68
00:03:16.710 --> 00:03:20.713
These are the same value, 8597 in both cases,

69
00:03:22.370 --> 00:03:25.250
and you can see that the IDs are the same.

70
00:03:25.250 --> 00:03:27.710
So why did this differ in lists

71
00:03:27.710 --> 00:03:30.990
when you created two empty lists, but not in integers?

72
00:03:30.990 --> 00:03:32.150
Python has this optimization

73
00:03:32.150 --> 00:03:33.750
where when an integer is created,

74
00:03:33.750 --> 00:03:36.103
then it won't recreate it if another one

75
00:03:36.103 --> 00:03:38.220
identical to it is used.

76
00:03:38.220 --> 00:03:41.850
So here Python knows that 8597 has already been created,

77
00:03:41.850 --> 00:03:44.150
so it doesn't create a new one.

78
00:03:44.150 --> 00:03:47.740
Integers are also immutable; you can't change them.

79
00:03:47.740 --> 00:03:49.570
New Python developers often believe

80
00:03:49.570 --> 00:03:53.000
that if you do something like 8598,

81
00:03:53.000 --> 00:03:55.070
now b should change as well

82
00:03:55.070 --> 00:03:58.140
because you have changed this number here,

83
00:03:58.140 --> 00:04:01.000
but that is not at all what's happened.

84
00:04:01.000 --> 00:04:02.730
This value was created first,

85
00:04:02.730 --> 00:04:05.050
and a is a name for that value.

86
00:04:05.050 --> 00:04:08.610
The name assignment happens with the equals sign,

87
00:04:08.610 --> 00:04:11.710
and here, you have created the value 8598,

88
00:04:11.710 --> 00:04:14.070
and you have given it the name a.

89
00:04:14.070 --> 00:04:17.360
However, b is still a name for 8597.

90
00:04:17.360 --> 00:04:19.360
That hasn't changed at all.

91
00:04:19.360 --> 00:04:21.690
However, if you do print these IDs,

92
00:04:21.690 --> 00:04:23.120
you can see that that's all correct,

93
00:04:23.120 --> 00:04:25.270
and they are now different.

94
00:04:25.270 --> 00:04:26.873
They are identical at the start,

95
00:04:28.290 --> 00:04:30.393
and then they differ afterwards.

96
00:04:31.410 --> 00:04:34.350
As I said earlier, most things in Python are mutable

97
00:04:34.350 --> 00:04:37.960
except for tuples, strings, integers, floats, and booleans.

98
00:04:37.960 --> 00:04:40.470
I believe that's most of the immutable ones.

99
00:04:40.470 --> 00:04:42.090
All the rest are mutable,

100
00:04:42.090 --> 00:04:42.970
and if you want to create

101
00:04:42.970 --> 00:04:46.050
an immutable class and immutable objects,

102
00:04:46.050 --> 00:04:48.220
just don't add any methods in them

103
00:04:48.220 --> 00:04:51.110
that can change the object's properties.

104
00:04:51.110 --> 00:04:52.930
For example, if you have a book object

105
00:04:52.930 --> 00:04:54.370
and you want it immutable,

106
00:04:54.370 --> 00:04:56.730
don't allow anybody to change

107
00:04:56.730 --> 00:04:59.023
the name property of that book.

108
00:04:59.920 --> 00:05:02.240
One more example for beginner Python developers

109
00:05:02.240 --> 00:05:05.270
is that if you create a string called hello

110
00:05:05.270 --> 00:05:06.610
and give it the name a,

111
00:05:06.610 --> 00:05:10.190
and then make b and give it the same value as a's value,

112
00:05:10.190 --> 00:05:11.960
then these will be the same.

113
00:05:11.960 --> 00:05:14.280
If you do plus equal,

114
00:05:14.280 --> 00:05:18.060
that is the same thing as doing equal a plus world,

115
00:05:18.060 --> 00:05:23.060
so you are reassigning the name a into this new string,

116
00:05:23.390 --> 00:05:28.170
but b will still be hello, so this will change the name a.

117
00:05:28.170 --> 00:05:30.913
It won't change the string hello.

118
00:05:31.836 --> 00:05:33.800
So that's something important to remember.

119
00:05:33.800 --> 00:05:36.800
Immutable types like strings, you can't change them.

120
00:05:36.800 --> 00:05:39.722
It's physically not possible in Python to change them,

121
00:05:39.722 --> 00:05:41.790
so whenever you are assigning,

122
00:05:41.790 --> 00:05:45.340
you're always going to be changing the existing name

123
00:05:45.340 --> 00:05:46.940
and not the value,

124
00:05:46.940 --> 00:05:48.970
and the same thing for tuples,

125
00:05:48.970 --> 00:05:50.883
integers, floats, and booleans.

126
00:05:52.620 --> 00:05:53.470
That's it for this video.

127
00:05:53.470 --> 00:05:56.810
I wanted to tell you a bit about mutability in Python.

128
00:05:56.810 --> 00:05:58.380
In the next one, we're going to learn

129
00:05:58.380 --> 00:06:00.480
why it's important to know about it.

130
00:06:00.480 --> 00:06:03.130
So stick with me, and I'll see you in the next video.

