1
00:00:05,240 --> 00:00:09,470
Welcome back, everyone, to part three of our continued discussion, object oriented programming.

2
00:00:09,770 --> 00:00:11,600
We're going to discuss methods.

3
00:00:11,900 --> 00:00:13,400
Let's head back to our code editor.

4
00:00:14,060 --> 00:00:14,300
All right.

5
00:00:14,300 --> 00:00:19,400
So here I am inside Visual Studio Code Editor Now, when we're talking about methods, they're really

6
00:00:19,400 --> 00:00:25,340
just functions defined inside the body of a class, and they are used to perform operations with the

7
00:00:25,340 --> 00:00:26,900
attributes of our objects.

8
00:00:27,380 --> 00:00:31,790
Now let's go ahead and create a class called Circle.

9
00:00:32,689 --> 00:00:35,060
So we have our instance of a circle class.

10
00:00:35,660 --> 00:00:40,190
And then for most classes, we're going to be creating an instantiation method or in its call.

11
00:00:41,190 --> 00:00:48,060
So double underscores, remember, and this is going to take in self and what are the attributes associated

12
00:00:48,060 --> 00:00:48,720
with a circle?

13
00:00:49,080 --> 00:00:55,530
Well, one of the attributes can be the radius, so we can have a user define the radius of a circle.

14
00:00:56,920 --> 00:01:00,970
So I can say radius and then pass in, oops, forgot the colon.

15
00:01:01,120 --> 00:01:01,630
There we go.

16
00:01:02,020 --> 00:01:02,920
Say self.

17
00:01:03,280 --> 00:01:08,740
The radius is equal to the radius passed in by the user.

18
00:01:09,190 --> 00:01:13,930
Now, if you want a default value for your radius, you can always say something like radius equal to

19
00:01:13,930 --> 00:01:21,100
one, and that will set a default value to one against the actual user doesn't define radius upon creating

20
00:01:21,100 --> 00:01:21,550
a circle.

21
00:01:22,060 --> 00:01:27,820
So now outside of this, I can say my circle is equal to an instance of circle, and if I wanted to,

22
00:01:27,820 --> 00:01:30,490
I could say something like radius.

23
00:01:31,590 --> 00:01:33,990
Equal to 20, for example.

24
00:01:34,650 --> 00:01:42,450
And then I could print out my circle and print out its specific radius, so it's going to run this just

25
00:01:42,450 --> 00:01:43,350
to check if it works.

26
00:01:46,310 --> 00:01:52,070
And now I see the output of 20, so I have this very simple circle class and then in order to create

27
00:01:52,070 --> 00:01:54,110
a circle, I do need to define its radius.

28
00:01:54,440 --> 00:01:56,540
Otherwise, we'll have a default radius of one.

29
00:01:57,170 --> 00:02:02,600
If we think back to class object attributes things that are true for any instance of the class, such

30
00:02:02,600 --> 00:02:10,460
as Circle, I could say that PI is a class object attribute, so I could say PI is equal to three point

31
00:02:10,639 --> 00:02:13,740
one for save that change.

32
00:02:13,760 --> 00:02:18,740
And so three point four makes sense to be a class object attribute because regardless of the radius

33
00:02:18,740 --> 00:02:21,560
of the circle, it should always have the same PI attribute.

34
00:02:21,620 --> 00:02:26,330
Three point one For now, let's talk about adding in more methods.

35
00:02:26,990 --> 00:02:33,110
This init method is technically a method within the actual circle class, but now we want to expand

36
00:02:33,320 --> 00:02:35,600
on things that are circle class can do.

37
00:02:36,110 --> 00:02:41,180
For example, let's imagine I wanted to calculate the area of a circle.

38
00:02:41,330 --> 00:02:47,260
I could create a method for that, so I could say something like the F area.

39
00:02:47,630 --> 00:02:53,330
And then to make it clear that I'm going to be performing this method using attributes from the instance

40
00:02:53,330 --> 00:03:00,350
of the class I pass in self here that essentially connects this function to be a method operating on

41
00:03:00,350 --> 00:03:00,920
the class.

42
00:03:01,760 --> 00:03:05,210
And here I can decide whether I want to print or return.

43
00:03:05,570 --> 00:03:10,310
Typically, methods are going to return in the same way functions usually a return they don't usually

44
00:03:10,310 --> 00:03:10,700
print.

45
00:03:11,120 --> 00:03:16,190
So to calculate the area of a circle, it's going to be its radius squared times PI.

46
00:03:17,030 --> 00:03:20,650
So I'm going to say return self-taught radius.

47
00:03:20,690 --> 00:03:28,010
Notice how I say self dot radius because I'm passing in self, which now allows me to have access to

48
00:03:28,010 --> 00:03:29,210
self, but radius up here.

49
00:03:29,600 --> 00:03:35,120
I can't just say radius by itself because I was only passed in in the in its method.

50
00:03:35,150 --> 00:03:39,170
It's not actually being passed tense area, which is why I say self, but radius.

51
00:03:39,710 --> 00:03:41,780
Now remember it's self that radius squared.

52
00:03:42,170 --> 00:03:47,330
So I could say times self radius and it's pi are square.

53
00:03:47,360 --> 00:03:49,130
So I do need to multiply it by PI.

54
00:03:49,970 --> 00:03:54,920
There's two ways I could do this because PI is a class object attribute.

55
00:03:55,580 --> 00:04:01,320
I actually have the ability to say circle that PI and I can do this circle that pi.

56
00:04:01,340 --> 00:04:01,790
There we go.

57
00:04:02,180 --> 00:04:06,980
I can do this for class object attributes because PI doesn't change regardless of the instance of the

58
00:04:06,980 --> 00:04:12,560
circle or what I can also do is, say, self-taught pi either is OK.

59
00:04:12,860 --> 00:04:17,570
Circle that PI basically shows somebody who's reading that method that, by the way, PI is a class

60
00:04:17,570 --> 00:04:21,079
object attribute, but either of these methods should work just fine.

61
00:04:21,620 --> 00:04:29,600
So no, again what this area method does, it takes its self and then it can use attributes in order

62
00:04:29,600 --> 00:04:30,830
to return something.

63
00:04:31,370 --> 00:04:34,610
So I'm going to define a circle with a radius of four.

64
00:04:35,150 --> 00:04:41,720
And then I'm going to call area and notice how I'm not actually passing in open and close parentheses

65
00:04:41,720 --> 00:04:42,440
into area.

66
00:04:42,800 --> 00:04:44,510
We'll see why in just a second.

67
00:04:44,510 --> 00:04:48,080
In fact, we should be calling parentheses, but I want to show you what happens when you don't.

68
00:04:48,710 --> 00:04:56,150
If I just run it like this, it says, Hey, this is a bound method, circled the area of this particular

69
00:04:56,150 --> 00:05:01,790
circle object at this point in your computer's memory, this area.

70
00:05:02,300 --> 00:05:05,180
Notice how it's not actually inside in it.

71
00:05:05,390 --> 00:05:10,970
In fact, area, just like a function, needs to be executed with open close parentheses.

72
00:05:11,390 --> 00:05:18,320
If you just say my circle dot area, that's not actually executing this function slash method.

73
00:05:19,130 --> 00:05:21,170
Instead, what it's doing is just reporting back to you.

74
00:05:21,500 --> 00:05:28,790
Hey, area is this bound method for a circle object created at this point in memory to actually execute?

75
00:05:29,710 --> 00:05:32,830
Area, I need to open close parentheses on it.

76
00:05:32,980 --> 00:05:39,340
Now, when I run this, I see that the area of the circle of Radius four is equal to fifty point two

77
00:05:39,340 --> 00:05:39,730
four.

78
00:05:40,450 --> 00:05:42,760
Let's go ahead and try to finding another method.

79
00:05:42,820 --> 00:05:44,470
Let's make this method for perimeter.

80
00:05:45,160 --> 00:05:48,340
In fact, try to pause the video and see if you can do this yourself.

81
00:05:49,030 --> 00:05:51,040
So how do we define perimeter?

82
00:05:51,550 --> 00:05:54,790
Well, we're going to say is create another method called perimeter.

83
00:05:55,420 --> 00:05:56,980
Again, it takes itself that way.

84
00:05:56,980 --> 00:05:58,600
I can access those attributes.

85
00:05:59,680 --> 00:06:04,330
And then I'm going to return the perimeter, which is just two times the radius, times PI.

86
00:06:04,840 --> 00:06:06,220
So I should be returning to.

87
00:06:07,270 --> 00:06:09,640
Times the radius, self-taught radius.

88
00:06:10,720 --> 00:06:17,680
Times softball pie, or if it's a class object attribute, you could also say circle that pie.

89
00:06:18,130 --> 00:06:19,600
It's up to you when you want to use.

90
00:06:20,110 --> 00:06:23,410
So let's now instead of calling area called the perimeter.

91
00:06:26,390 --> 00:06:27,320
Save that change.

92
00:06:28,340 --> 00:06:33,030
Go ahead and run this now I can see the perimeter being reported back and again, this can be circled

93
00:06:33,030 --> 00:06:37,400
that pie, or it could also be soft that pie.

94
00:06:38,060 --> 00:06:41,180
Now you may be wondering which one's better to use circled up higher self the pie.

95
00:06:41,450 --> 00:06:46,940
Some people sometimes they prefer to use circle because it lets someone know as they're reading this

96
00:06:46,940 --> 00:06:47,330
method.

97
00:06:47,690 --> 00:06:49,400
Oh, by the way, circle that pie.

98
00:06:49,580 --> 00:06:52,790
That's a class object attribute not defined by the user.

99
00:06:53,240 --> 00:06:57,410
Other people like the consistency of just having everything self.

100
00:06:57,980 --> 00:07:02,210
Either way, it's going to work the same, so I wouldn't worry about it too much for our use cases.

101
00:07:03,310 --> 00:07:09,160
In fact, we're not going to see too many class object attributes as we continue to work with our own

102
00:07:09,160 --> 00:07:12,760
object oriented programming creation within the context of Django.

103
00:07:13,450 --> 00:07:13,840
All right.

104
00:07:14,320 --> 00:07:16,920
So let's again review our methods.

105
00:07:16,930 --> 00:07:22,120
We have this init method, which we have for most of our instances for classes and then we can define

106
00:07:22,120 --> 00:07:23,320
our own methods.

107
00:07:23,680 --> 00:07:26,560
These are really simple methods that I did all on one line.

108
00:07:26,890 --> 00:07:33,580
But keep in mind, I could create new variables here, so I could say something like, Hey, inside

109
00:07:33,580 --> 00:07:34,270
of this area.

110
00:07:35,190 --> 00:07:44,160
Go ahead and do something like results is equal to self thought radius squared, and then maybe I want

111
00:07:44,160 --> 00:07:49,590
to update results within results as equal to results times self-taught pi.

112
00:07:49,770 --> 00:07:51,300
And then here return.

113
00:07:52,460 --> 00:07:57,650
Result after it got updated, so this is the same thing, it's pi times our squared and here I'm just

114
00:07:57,650 --> 00:07:58,610
returning the result.

115
00:07:58,910 --> 00:08:04,610
But just keep in mind, I'm now able to use these variables that I want within the context of this method.

116
00:08:04,640 --> 00:08:09,710
Note that I don't need to say self here because this is a little temporary variable that only lives

117
00:08:09,950 --> 00:08:14,540
within the scope of the area method when we're talking about things like self.

118
00:08:14,930 --> 00:08:22,250
Those are attributes of the object that can be accessed as long as we say self inside the actual method

119
00:08:22,250 --> 00:08:25,040
of call, which by convention we typically always do.

120
00:08:25,940 --> 00:08:26,330
All right.

121
00:08:26,690 --> 00:08:28,660
So those are the way method works.

122
00:08:28,670 --> 00:08:34,159
You can think of them as just functions that you're defining inside of the class that can then access

123
00:08:34,700 --> 00:08:39,110
attributes of the class in order to perform some sort of calculation.

124
00:08:39,230 --> 00:08:45,980
And keep in mind, you can also make method calls that update the actual attributes themselves.

125
00:08:46,400 --> 00:08:51,260
It's not as common as grabbing the attribute, but you could go ahead and do something like that.

126
00:08:51,620 --> 00:08:59,210
So I'm going to create kind of a silly method here, and this silly method is going to be called double

127
00:08:59,210 --> 00:08:59,780
radius.

128
00:09:01,290 --> 00:09:02,900
And then takes in self.

129
00:09:03,720 --> 00:09:06,300
And what this methods actually going to do is what it says.

130
00:09:06,300 --> 00:09:09,600
It's going to take the existing radius and then double it.

131
00:09:10,260 --> 00:09:20,520
So it's going to say self that radius is equal to so that radius times to note what's happening here.

132
00:09:20,970 --> 00:09:29,010
I'm saying grab the current radius, multiply it by two and then reset the existing radius to that.

133
00:09:30,180 --> 00:09:35,490
Keep in mind, this method doesn't really make sense to return anything, I could maybe print out a

134
00:09:35,490 --> 00:09:39,510
result saying something like radius has been.

135
00:09:40,430 --> 00:09:41,750
Doubled to.

136
00:09:42,320 --> 00:09:47,330
And maybe use f string literal formatting here, that may be something I want to see reported back up

137
00:09:47,330 --> 00:09:51,350
to user and then I would simply say self-taught radius here.

138
00:09:51,770 --> 00:09:58,610
This is just to give you the idea that the methods themselves can permanently change the attribute values.

139
00:09:59,150 --> 00:10:00,680
So let's go ahead and check this out.

140
00:10:00,710 --> 00:10:05,360
I'll create a circle for radius of four and then instead of calling perimeter, I'm going to say.

141
00:10:06,650 --> 00:10:12,320
Double the radius and here, I don't need to actually print anything up because nothing's being returned.

142
00:10:13,850 --> 00:10:21,050
So I'm calling my circle double radius, which should I expect to grab the current radius and reset

143
00:10:21,050 --> 00:10:22,730
it to the double of it?

144
00:10:23,090 --> 00:10:27,200
Then it's going to report back that the radius has been doubled, although keep in mind, it doesn't

145
00:10:27,200 --> 00:10:29,690
actually return anything, it's just printing out the report.

146
00:10:30,230 --> 00:10:36,440
This is just to show you that methods can effectively update within attributes, and they can also take

147
00:10:36,440 --> 00:10:40,670
in arguments and then do whatever they want to the current existing object.

148
00:10:41,210 --> 00:10:45,080
So I go ahead and run this and it says, Hey, radius has been doubled to eight.

149
00:10:45,350 --> 00:10:48,830
In fact, let's go ahead and prove it to ourselves by saying print.

150
00:10:50,410 --> 00:10:57,340
My circle, that radius, which I should also see, is now eight after I had initially defined that.

151
00:10:57,340 --> 00:10:57,790
As for?

152
00:10:59,410 --> 00:11:01,220
And there it is, Radius has been doubled to eight.

153
00:11:01,240 --> 00:11:06,910
And so when they print out that radius, it has been indeed updated to be eight methods.

154
00:11:06,940 --> 00:11:09,430
It should also be pointed out, can take their own parameters.

155
00:11:09,880 --> 00:11:13,150
So now this is going to be called multiply.

156
00:11:14,140 --> 00:11:20,680
Radius, and now I'm going to take in a parameter to multiply by, so some numbers multiply.

157
00:11:20,710 --> 00:11:24,160
And so instead of saying self-built radius times two, I'll say.

158
00:11:25,250 --> 00:11:33,200
Self-taught radius times the number, and then I'll say radius has been changed to the updated now self-taught

159
00:11:33,200 --> 00:11:33,620
radius.

160
00:11:34,070 --> 00:11:38,510
So instead of calling double radius here, I'll say multiply radius.

161
00:11:38,960 --> 00:11:44,760
Now keep in mind, multiply radius as visual studio codes telling us requires some number to be passed

162
00:11:44,780 --> 00:11:44,960
in.

163
00:11:45,680 --> 00:11:46,430
Let's see what happens.

164
00:11:46,430 --> 00:11:49,640
If I don't pass in that number, I should expect an error.

165
00:11:50,660 --> 00:11:51,320
And there's the error.

166
00:11:51,320 --> 00:11:56,780
It says, Hey, multiply radius is missing that require two positional argument number, so I can pass

167
00:11:56,780 --> 00:11:57,140
that in.

168
00:11:58,240 --> 00:11:58,960
Let's say 20.

169
00:12:00,580 --> 00:12:05,950
I run this an hour radius has been changed to 80, and I can see the uptick here four times 20 is 80.

170
00:12:06,340 --> 00:12:12,760
That's just to give you the idea that methods can accept their own arguments and then use those arguments

171
00:12:12,760 --> 00:12:13,330
inside.

172
00:12:13,780 --> 00:12:19,630
You should also note how this particular argument not being within in it did not require something like

173
00:12:19,630 --> 00:12:25,000
soft, but no, because I'm only using this number at the scope of multiplied radius.

174
00:12:25,600 --> 00:12:30,670
I'm going to permanently affect the attribute radius, but I don't need to bother, say this particular

175
00:12:30,670 --> 00:12:35,560
number as some sort of attribute of the circle object because it's just a number that the user happened

176
00:12:35,560 --> 00:12:36,160
to pass in.

177
00:12:37,000 --> 00:12:39,220
OK, that's it for simple methods.

178
00:12:39,310 --> 00:12:41,380
Coming up next, we'll talk about inheritance.

