1
00:00:05,260 --> 00:00:07,480
Welcome back, everyone, to part two of our discussion of.

2
00:00:08,500 --> 00:00:11,050
Let's get started actually coding and creating a class.

3
00:00:11,590 --> 00:00:11,860
All right.

4
00:00:11,860 --> 00:00:14,050
So here I am inside visual studio code.

5
00:00:14,530 --> 00:00:19,000
Let's begin by understanding that in Python, everything is actually an object.

6
00:00:19,330 --> 00:00:20,350
How can I prove this to you?

7
00:00:20,530 --> 00:00:26,740
Well, I'm going to say that NUM is equal to 10, and I'm going to say print.

8
00:00:27,190 --> 00:00:35,830
The type of NUM wear type is a built in function within Python that reports back the type of object

9
00:00:35,830 --> 00:00:36,730
that you passed into it.

10
00:00:37,150 --> 00:00:44,860
So if I save those changes and I run this code, I get back Class A. For an integer class, less changes

11
00:00:44,860 --> 00:00:51,940
to be something like ten point three saved that change and run this and I see this as a float class

12
00:00:51,940 --> 00:00:54,790
object and maybe less changes to be a string.

13
00:00:55,360 --> 00:00:58,510
So we'll say 200, but it's around that in quotes.

14
00:00:59,230 --> 00:01:03,070
Next, I run this and I can see this is class STR for string.

15
00:01:03,640 --> 00:01:06,520
Now, clearly everything in Python is an object.

16
00:01:06,850 --> 00:01:10,810
Now the question arises How do we get to define our own objects?

17
00:01:10,960 --> 00:01:14,410
And that's where we have to use the class keyword and to come back here.

18
00:01:14,800 --> 00:01:16,120
Let's go ahead and clear this out.

19
00:01:16,570 --> 00:01:19,820
And let's begin by creating probably the simplest class possible.

20
00:01:19,840 --> 00:01:25,780
I call the class keyword and then by convention classes are capitalized and follow camel casing.

21
00:01:26,350 --> 00:01:30,010
So we're going to call this our sample class open and close parentheses.

22
00:01:30,640 --> 00:01:33,370
And keep in mind that you don't technically need parentheses here.

23
00:01:33,550 --> 00:01:37,570
You could also just say Colon, but I like open and close parentheses.

24
00:01:38,080 --> 00:01:39,060
And I'll show you why.

25
00:01:39,070 --> 00:01:40,750
Later on, we learn about inheritance.

26
00:01:40,750 --> 00:01:45,130
But again, keep in mind, technically speaking, you don't need these parentheses here, which is why

27
00:01:45,160 --> 00:01:47,590
sometimes you don't actually see this and documentation.

28
00:01:48,070 --> 00:01:50,710
But moving along, what I'll do is I'll simply put pass.

29
00:01:52,030 --> 00:01:54,820
So this is just a keyword in Python says Pass.

30
00:01:54,820 --> 00:01:55,690
Don't do anything.

31
00:01:55,780 --> 00:02:00,550
Essentially, this class does nothing except define itself as a sample class.

32
00:02:01,030 --> 00:02:04,120
Let's make what's known as an instance of a class.

33
00:02:04,390 --> 00:02:09,100
So I'm going to say now NUM is equal to an instance of sample.

34
00:02:09,220 --> 00:02:12,130
So I say sample open, close parentheses.

35
00:02:12,580 --> 00:02:15,580
And now let's print out the type of NUM.

36
00:02:15,970 --> 00:02:18,400
Notice how similar this is what we just said previously.

37
00:02:18,610 --> 00:02:23,620
Except this time, instead of calling this a float or integer or string, we're actually defining it

38
00:02:23,620 --> 00:02:25,510
as our own user defined class.

39
00:02:25,960 --> 00:02:28,570
Also, be very careful to note my indentation here.

40
00:02:28,990 --> 00:02:34,270
Classes are kind of like functions in the sense that everything indented is part of the class.

41
00:02:34,690 --> 00:02:39,610
Here I have this in line with the class call, and if you're confused, you can always use the collapse

42
00:02:39,610 --> 00:02:43,030
method here to see what code is actually inside the class.

43
00:02:43,570 --> 00:02:50,110
But now I saved this and I run this and I can see it says, Hey, this is class main.

44
00:02:50,320 --> 00:02:56,170
The sample main is actually a reference to the fact that it's running within the same Python file that

45
00:02:56,170 --> 00:02:57,010
it was defined then.

46
00:02:57,400 --> 00:03:01,210
And then sample is just telling you this is an instance of the sample class.

47
00:03:01,570 --> 00:03:01,930
All right.

48
00:03:02,320 --> 00:03:07,900
So now we understand this idea that in Python, everything is an object, and I can use this class keyword

49
00:03:08,170 --> 00:03:11,260
to actually define my own objects.

50
00:03:11,560 --> 00:03:12,670
So what's next?

51
00:03:13,060 --> 00:03:16,120
Well, we need to do is sometimes to find attributes.

52
00:03:16,510 --> 00:03:19,240
So let me show you how to define attributes within a class.

53
00:03:19,720 --> 00:03:23,080
What I'm going to do is here, inside class sample, I'm going to say.

54
00:03:24,430 --> 00:03:29,860
And instantiation method, which is a method that gets run every time you create a new instance of an

55
00:03:29,860 --> 00:03:38,710
object and it's thief underscore underscore I and I t underscore, underscore and keep in mind that

56
00:03:38,710 --> 00:03:41,170
there's two sets of underscores on each side here.

57
00:03:41,380 --> 00:03:46,390
Really common beginner mistake is that they only put one underscore and here what I'm going to do is

58
00:03:46,390 --> 00:03:49,240
I'm going to pass in the self keyword.

59
00:03:49,840 --> 00:03:52,720
The self keyword is this special keyword.

60
00:03:52,900 --> 00:03:56,620
Technically speaking, it doesn't need to be self, but by convention, we always use self.

61
00:03:56,980 --> 00:04:02,230
And this basically allows Python to understand that this particular method that we're creating and this

62
00:04:02,230 --> 00:04:04,990
is a special one called in it for instantiation.

63
00:04:05,350 --> 00:04:11,800
What's going to happen is when we actually create a instance of sample class, the self keyword is going

64
00:04:11,800 --> 00:04:17,690
to allow me to actually refer to attributes of the class from any other method in that class.

65
00:04:17,740 --> 00:04:21,820
So we're going to say self here and then I'm going to pass in one more parameter.

66
00:04:22,270 --> 00:04:25,840
And let's go ahead and have this say something like name.

67
00:04:26,650 --> 00:04:31,960
So now I can actually pass in a name upon creating an instance of sample class.

68
00:04:32,620 --> 00:04:36,790
And then here we're going to say self-taught name is equal to name.

69
00:04:37,390 --> 00:04:44,560
What I want to make clear here is if I'm attaching self-taught name to this particular variable, what

70
00:04:44,560 --> 00:04:51,010
this does is it allows self but name to later on be used within any method that we end up creating within

71
00:04:51,010 --> 00:04:54,700
the class name on this side without a self attached.

72
00:04:54,970 --> 00:05:00,310
Basically, it refers to the fact that the user, upon creating a sample class, will have to provide

73
00:05:00,310 --> 00:05:00,820
a name.

74
00:05:01,630 --> 00:05:03,400
So let me show you what I mean by that.

75
00:05:03,430 --> 00:05:07,930
I'm actually going to start calling this X just so we don't get confused with any numbers we pass in.

76
00:05:08,530 --> 00:05:13,030
And if I just run this, I should expect to see an error.

77
00:05:13,390 --> 00:05:14,830
So again, I have my class.

78
00:05:14,830 --> 00:05:21,310
I created this instantiation method in it call and it says that you have to pass in some name parameter

79
00:05:21,310 --> 00:05:23,830
and then it gets assigned to self that name attribute.

80
00:05:24,310 --> 00:05:31,450
If you just run it like this, we should get an error and it says, Hey, type error in it is missing

81
00:05:31,450 --> 00:05:33,790
one required positional argument called name.

82
00:05:34,240 --> 00:05:40,060
So now you have to actually say a name here, and we have been super clear or explicit on what this

83
00:05:40,060 --> 00:05:40,960
name actually is.

84
00:05:41,230 --> 00:05:43,150
But let's just assume that they kind of want a string.

85
00:05:44,110 --> 00:05:44,920
So I'll say that.

86
00:05:45,700 --> 00:05:51,610
And now when I run this, I can see that it's still a class main that sample, but now it has this attribute.

87
00:05:52,090 --> 00:05:57,100
And how can I actually access this attribute once this instance of the class is created?

88
00:05:57,640 --> 00:06:04,120
Well, I could say print type X, but now I can also do the following I can say print x dot.

89
00:06:05,220 --> 00:06:06,150
And then the name.

90
00:06:07,510 --> 00:06:08,110
Say that.

91
00:06:09,470 --> 00:06:11,600
Run this and now I get to see the result.

92
00:06:11,660 --> 00:06:18,260
Jose, so now we understand that if I have an attribute as part of a class call, I use Dot and then

93
00:06:18,260 --> 00:06:23,840
the attribute name and notice the similarity here we say in between saying the actual variable name,

94
00:06:24,110 --> 00:06:26,740
variable name, dot name and then self that name.

95
00:06:26,750 --> 00:06:32,330
So this syntax is reflective of what you're going to do after you instantiate that instance of the class.

96
00:06:32,720 --> 00:06:34,400
In fact, let's start changing this.

97
00:06:34,700 --> 00:06:40,770
So maybe we're trying to create a student class, so we're trying to organize a school.

98
00:06:40,820 --> 00:06:47,720
And so I create this student class and a student could have a name, and maybe a student also has a

99
00:06:47,720 --> 00:06:48,290
GPA.

100
00:06:49,070 --> 00:06:54,740
And then I want to make sure that when they pass on that, I'm actually storing it to be part of an

101
00:06:54,740 --> 00:06:55,730
attribute of the student.

102
00:06:55,730 --> 00:07:01,250
So I can say salta GPA is equal to the GPA that's passed into the user.

103
00:07:01,850 --> 00:07:04,700
So now I can actually then create instances of these objects.

104
00:07:04,700 --> 00:07:11,030
For example, I'm going to create student one as equal to Jose and will be generous myself to give myself

105
00:07:11,030 --> 00:07:14,010
a 4.0 GPA, whether or not that was actually true.

106
00:07:14,030 --> 00:07:19,580
We'll leave it up to history to decide, and we're going to say Student two is equal to and I remember

107
00:07:19,580 --> 00:07:20,840
sample's no longer the fine.

108
00:07:21,680 --> 00:07:23,300
So I have to actually call this students.

109
00:07:23,660 --> 00:07:27,200
Help me expand our space here a little bit so you can see everything I'm doing.

110
00:07:27,710 --> 00:07:33,770
So we have our student class now takes a name and GPA as attributes that are defined upon creating a

111
00:07:33,770 --> 00:07:34,250
new student.

112
00:07:34,700 --> 00:07:38,630
So Student one is named Jose GPA for Let's Create Another Student.

113
00:07:40,600 --> 00:07:42,910
The name will be say me, me.

114
00:07:44,510 --> 00:07:47,230
And this is going to be, let's say, three point five.

115
00:07:47,970 --> 00:07:52,400
OK, so if you just create those students, nothing actually really happens here.

116
00:07:52,760 --> 00:07:55,970
So if you're own Python, example that pi expanding this a little bit.

117
00:07:57,000 --> 00:08:00,390
You can see we don't actually get any sort of output because I haven't printed anything.

118
00:08:00,750 --> 00:08:06,270
So what I can do is let's go ahead and say print student one dot GPA.

119
00:08:07,950 --> 00:08:08,880
Save that change.

120
00:08:09,420 --> 00:08:15,750
Run this now I can see 4.0, and later on we'll actually discuss what actually happens if you want something

121
00:08:15,750 --> 00:08:16,590
more displayed.

122
00:08:16,830 --> 00:08:23,280
Because if I just say print stu one, I don't actually see name or it just says, Hey, this is a student

123
00:08:23,280 --> 00:08:25,230
object at this point in memory.

124
00:08:25,260 --> 00:08:26,400
This is not super useful.

125
00:08:26,670 --> 00:08:31,950
So later on in the series of lectures, we will discuss how to create customization for what is printed

126
00:08:31,950 --> 00:08:32,150
out.

127
00:08:32,159 --> 00:08:32,970
But just keep that in mind.

128
00:08:32,970 --> 00:08:37,679
If you actually print out an instance of an object, then you're going to see, Hey, this is a student

129
00:08:37,679 --> 00:08:39,990
object at this point in memory at your computer.

130
00:08:41,309 --> 00:08:48,040
So one thing to notice here is, unlike method calls, attributes are not actually called with parentheses.

131
00:08:48,060 --> 00:08:51,390
So I just said, Stu, that name, I did not say name.

132
00:08:51,600 --> 00:08:52,800
Open close parentheses.

133
00:08:53,190 --> 00:08:58,140
Open close princes later on will be used when we actually create new methods for that class.

134
00:08:58,500 --> 00:09:05,310
But right now, we can think of attributes as descriptors of an instance of the class, like a name

135
00:09:05,460 --> 00:09:08,550
and a describe an instance of a student.

136
00:09:09,030 --> 00:09:15,060
Now there may be something called a class object attribute, which is going to be an attribute that

137
00:09:15,060 --> 00:09:20,610
should be the same regardless of the actual instance of the class.

138
00:09:20,880 --> 00:09:26,970
So, for example, what would be true for any student at the school that maybe their name will be different,

139
00:09:26,970 --> 00:09:28,640
but their GPA will also be different.

140
00:09:28,650 --> 00:09:31,830
But I want something that's going to be true for any instance of a student.

141
00:09:32,220 --> 00:09:33,870
So what could I do for something like that?

142
00:09:34,140 --> 00:09:37,800
Maybe I could just say that all students are going to be human students.

143
00:09:37,920 --> 00:09:39,750
So this is not a school for dogs.

144
00:09:40,200 --> 00:09:46,350
So maybe I say something like species is equal to and then say human.

145
00:09:46,920 --> 00:09:48,510
Or maybe they're all on planet Earth.

146
00:09:48,690 --> 00:09:54,060
So I say, OK, for all these students, their planet is going to be Earth.

147
00:09:55,140 --> 00:09:56,880
So what's kind of special to note here?

148
00:09:57,330 --> 00:10:02,340
You should know that this is known as a class object attribute.

149
00:10:02,850 --> 00:10:11,640
So this is a class object attribute and this is different than name and GPA here, which are just attributes.

150
00:10:11,850 --> 00:10:12,750
So what's the difference?

151
00:10:13,080 --> 00:10:14,760
The difference is for one.

152
00:10:14,790 --> 00:10:17,790
This actually doesn't go inside the init call.

153
00:10:18,120 --> 00:10:22,830
If you want an attribute that is going to be the same for every instance of an object you create.

154
00:10:23,130 --> 00:10:25,740
You actually put it outside the init call.

155
00:10:26,190 --> 00:10:30,760
The other thing you should know is it doesn't actually need self-talk.

156
00:10:31,530 --> 00:10:38,310
Why does it not need self, but, well, notice that this is going outside of any of these method calls

157
00:10:38,310 --> 00:10:39,000
that we're creating.

158
00:10:39,360 --> 00:10:45,270
So hence, it's kind of obvious to Python that, yes, planet is associated with an instance of student.

159
00:10:45,630 --> 00:10:48,210
It's not hidden within some sort of method.

160
00:10:48,570 --> 00:10:54,960
And the reason self is here inside this method is to make sure you have the capability to connect these

161
00:10:54,960 --> 00:11:01,230
user defined attributes to an attribute that you can later on use with an other method of calls.

162
00:11:01,770 --> 00:11:08,730
So this is a class object attribute and not what happens when I save this code and I run this again.

163
00:11:09,630 --> 00:11:10,950
I don't get any sort of error.

164
00:11:10,980 --> 00:11:16,710
Remember student one, their name is Jose, but what I can also do is say student one now and ask for

165
00:11:16,740 --> 00:11:18,750
what planet are they from?

166
00:11:20,090 --> 00:11:22,190
Run this, and now I can see it's Earth.

167
00:11:22,610 --> 00:11:26,720
Now here for student one, I didn't have to actually declare planet.

168
00:11:26,750 --> 00:11:32,420
In fact, we should expect that we never have to declare planet because the whole point of a class object

169
00:11:32,420 --> 00:11:36,860
attribute is that it's going to be the same for any instance of a students.

170
00:11:37,040 --> 00:11:41,450
And again, you can always use the collapse mechanism here to give yourself a little more space to view

171
00:11:41,720 --> 00:11:44,900
what the class has as far as attributes and methods.

172
00:11:45,410 --> 00:11:47,690
OK, so that's pretty much it.

173
00:11:47,690 --> 00:11:52,610
For this lecture, we created our first class, and along with that, we also created attributes.

174
00:11:52,940 --> 00:11:58,700
What I'm going to do is just from scratch create another class of something kind of related to students

175
00:11:58,940 --> 00:12:02,480
just to get us a little familiar with the idea of creating a class.

176
00:12:02,930 --> 00:12:09,950
Let's imagine I'm creating a secret agent class, so I'm running the CIA or some sort of secret intelligence

177
00:12:09,950 --> 00:12:10,610
agency.

178
00:12:11,030 --> 00:12:12,670
And I want to create an agent class.

179
00:12:12,670 --> 00:12:14,270
So I'm going to say class agent.

180
00:12:16,260 --> 00:12:24,780
And maybe since I'm running this at our national organization, I know that the birth or place of origin

181
00:12:24,780 --> 00:12:28,590
for all these agents is going to be United States or something like that.

182
00:12:29,100 --> 00:12:32,220
So we're going to see Origin is USA.

183
00:12:32,970 --> 00:12:36,150
So origin as USA, that means all these agents.

184
00:12:36,390 --> 00:12:40,120
I'm going to say their origin is USA, regardless of what's actually passed it.

185
00:12:40,770 --> 00:12:46,950
Then when I want to actually create an instance of an agent class automatically, the in its method

186
00:12:46,950 --> 00:12:47,790
is going to be called.

187
00:12:48,210 --> 00:12:52,080
So in that method is called upon creating an instance of agent.

188
00:12:52,680 --> 00:12:55,680
So we pass in self so we can begin to assign attributes.

189
00:12:56,280 --> 00:12:57,630
And then what else can we do here?

190
00:12:57,660 --> 00:13:01,110
Well, I can say something like maybe the agents are going to have a name.

191
00:13:01,530 --> 00:13:06,270
Maybe they have a height, and maybe they also have a weight or something like that.

192
00:13:06,690 --> 00:13:07,050
Well.

193
00:13:08,170 --> 00:13:12,910
What I do, then, is in order to accept these are these parameter calls and assign them as attributes

194
00:13:12,910 --> 00:13:17,350
as I buy conventions say the same parameter name just with herself in front of it.

195
00:13:17,590 --> 00:13:21,430
So if that name is name and then we're going to say soft that height.

196
00:13:22,490 --> 00:13:23,450
Is height.

197
00:13:24,450 --> 00:13:25,830
And then self thought, wait.

198
00:13:27,770 --> 00:13:28,820
Is wait here.

199
00:13:29,690 --> 00:13:32,480
OK, so here we have again, a very simple class.

200
00:13:33,140 --> 00:13:36,260
Every instance of an agent is going to be Origin USA.

201
00:13:36,770 --> 00:13:40,700
And then when you create an agent, you have to define their name, their height and their weight.

202
00:13:41,060 --> 00:13:43,130
So we also think of this maybe as like a patient or something.

203
00:13:43,370 --> 00:13:49,700
If I wanted to change the class, so now I'm going to create an instance of an agent so I can say X,

204
00:13:49,700 --> 00:13:52,420
for instance, is equal to an instance of an agent.

205
00:13:52,460 --> 00:13:55,370
I remember I need to define their name, their height and their weight.

206
00:13:55,460 --> 00:14:02,390
So I could say something like Kosi weight or height two six and then weights is 170 or something like

207
00:14:02,390 --> 00:14:02,630
that.

208
00:14:03,170 --> 00:14:05,120
We're not worried too much about the metrics right now.

209
00:14:05,270 --> 00:14:06,830
And then I can say prints.

210
00:14:07,940 --> 00:14:09,680
X name, for instance.

211
00:14:10,960 --> 00:14:14,090
Save that, and when you run this code, you should see now, Josie.

212
00:14:14,470 --> 00:14:19,150
So he created an instance of an agent that I can now actually use.

213
00:14:19,840 --> 00:14:23,620
Now I can also update the actual attributes.

214
00:14:23,860 --> 00:14:26,670
So let's say the agent lost some weight.

215
00:14:26,680 --> 00:14:36,790
I could say x dot weight is equal to 160 and then print out x that weight.

216
00:14:37,210 --> 00:14:42,760
So this is one way of actually grabbing an attribute and then updating it just to prove to you that

217
00:14:42,760 --> 00:14:43,600
the update works.

218
00:14:44,050 --> 00:14:45,880
Before I update, I will print.

219
00:14:46,900 --> 00:14:47,720
It's that way.

220
00:14:47,740 --> 00:14:53,320
So notice what I'm doing, I'm initially defining Jose having weight 170, I print out that weight.

221
00:14:53,590 --> 00:14:56,500
The uptake happens and then I print out the new updated weight.

222
00:14:56,770 --> 00:15:01,000
So I start see 170 and then 160 to show that the update worked.

223
00:15:02,180 --> 00:15:07,250
And there it is, 170 upon creation, and then later on, I updated it using one 60.

224
00:15:07,640 --> 00:15:14,630
So clearly there's this idea of I'm able to grab attributes, report them, and I can also update attributes.

225
00:15:15,050 --> 00:15:20,390
Keep in mind, we shouldn't really keep keeping the effect of updating class object attributes.

226
00:15:20,390 --> 00:15:21,890
That's not really what they're there for.

227
00:15:22,640 --> 00:15:25,340
So that is the basics of creating a class.

228
00:15:25,670 --> 00:15:29,030
The instantiation method that can then allow us to define attributes.

229
00:15:29,420 --> 00:15:33,560
Coming up next, we're going to learn about methods which are essentially functions defined inside the

230
00:15:33,560 --> 00:15:34,550
body of a class.

231
00:15:34,940 --> 00:15:39,260
And then it's going to make a lot more sense why we can use things like self that name instead of just

232
00:15:39,260 --> 00:15:39,620
name.

233
00:15:40,070 --> 00:15:41,540
OK, I'll see you at the next lecture.

