1
1

00:00:00,520  -->  00:00:01,940
<v Instructor>Hi guys and welcome back.</v>
2

2

00:00:01,940  -->  00:00:04,600
In this video, we're going to talk about static methods
3

3

00:00:04,600  -->  00:00:06,140
and class methods.
4

4

00:00:06,140  -->  00:00:08,990
These can be a little bit confusing so let's get started.
5

5

00:00:08,990  -->  00:00:12,550
Here, we've got a class that I have called ClassTest.
6

6

00:00:12,550  -->  00:00:15,040
This is just to show you how these things work.
7

7

00:00:15,040  -->  00:00:18,160
And then we have defined a method.
8

8

00:00:18,160  -->  00:00:21,630
We know it's a method because it is a function,
9

9

00:00:21,630  -->  00:00:23,280
which starts with a def keyword,
10

10

00:00:23,280  -->  00:00:27,040
and it has self as a first parameter.
11

11

00:00:27,040  -->  00:00:30,950
All functions inside the class that use the object
12

12

00:00:30,950  -->  00:00:33,380
as the first parameter,
13

13

00:00:33,380  -->  00:00:35,673
are called instance methods.
14

14

00:00:36,770  -->  00:00:38,860
And this instance_method,
15

15

00:00:38,860  -->  00:00:41,030
prints that you've called the instance_method.
16

16

00:00:41,030  -->  00:00:44,210
How would you call this method if you wanted to call it?
17

17

00:00:44,210  -->  00:00:46,640
You have two options.
18

18

00:00:46,640  -->  00:00:50,610
Okay, the first option is to do something like,
19

19

00:00:50,610  -->  00:00:55,610
test = ClassTest, and create a new object of type ClassTest.
20

20

00:00:56,780  -->  00:00:59,780
Notice that we don't have an init method in this class,
21

21

00:00:59,780  -->  00:01:00,810
but that's okay.
22

22

00:01:00,810  -->  00:01:02,105
If you don't have an init method,
23

23

00:01:02,105  -->  00:01:03,071
that just means that you
24

24

00:01:03,071  -->  00:01:05,380
don't have any initialization to do,
25

25

00:01:05,380  -->  00:01:08,470
but the self is still created
26

26

00:01:08,470  -->  00:01:10,310
and you still get a new object.
27

27

00:01:10,310  -->  00:01:13,050
And make it equal to your variable.
28

28

00:01:13,050  -->  00:01:14,210
Here, we're creating a new object
29

29

00:01:14,210  -->  00:01:18,170
and you can do test.instance_method, or as we know it,
30

30

00:01:18,170  -->  00:01:20,770
you can ClassTest.instance_method
31

31

00:01:20,770  -->  00:01:22,630
with test as the argument.
32

32

00:01:22,630  -->  00:01:24,610
Both of these things are exactly the same.
33

33

00:01:24,610  -->  00:01:27,670
If you run this code, you'll get that you've called
34

34

00:01:27,670  -->  00:01:30,430
the instance_method of this object.
35

35

00:01:30,430  -->  00:01:33,770
Notice that we are printing the object self here,
36

36

00:01:33,770  -->  00:01:35,630
and as we saw earlier on,
37

37

00:01:35,630  -->  00:01:38,590
you get back this ugly print out.
38

38

00:01:38,590  -->  00:01:40,470
If you wanted it to be a little bit nicer,
39

39

00:01:40,470  -->  00:01:44,050
you would have to implement your str or repr methods,
40

40

00:01:44,050  -->  00:01:45,050
one of the two.
41

41

00:01:45,050  -->  00:01:49,150
But for now, we have called the instance_method two times.
42

42

00:01:49,150  -->  00:01:52,800
It is called an instance_method because you call it
43

43

00:01:52,800  -->  00:01:55,430
on a class instance.
44

44

00:01:55,430  -->  00:01:58,600
The name instance, it means the same thing, an object.
45

45

00:01:58,600  -->  00:02:00,980
What we're doing here is we're creating an object
46

46

00:02:00,980  -->  00:02:01,880
of type ClassTest.
47

47

00:02:03,220  -->  00:02:04,530
You can also say that you're creating
48

48

00:02:04,530  -->  00:02:06,410
an instance of ClassTest.
49

49

00:02:06,410  -->  00:02:08,170
They mean the same thing.
50

50

00:02:08,170  -->  00:02:10,653
That is why these are called instance methods.
51

51

00:02:11,750  -->  00:02:16,200
Instance methods need the object in order to call them.
52

52

00:02:16,200  -->  00:02:20,290
The next type is called a class method.
53

53

00:02:20,290  -->  00:02:23,280
Here, you can define another method.
54

54

00:02:23,280  -->  00:02:28,280
But now instead of the instance, or the object, self,
55

55

00:02:28,360  -->  00:02:31,000
it actually takes a different parameter
56

56

00:02:31,000  -->  00:02:33,610
that we usually in Python, call cls,
57

57

00:02:33,610  -->  00:02:35,550
but you can call this parameter whatever you want,
58

58

00:02:35,550  -->  00:02:37,470
the value will still be the same no matter
59

59

00:02:37,470  -->  00:02:39,520
what you call this parameter.
60

60

00:02:39,520  -->  00:02:42,740
What this parameter will be in a class method,
61

61

00:02:42,740  -->  00:02:45,870
that is when you put this at @classmethod on top
62

62

00:02:45,870  -->  00:02:50,573
of the method definition, is this will be the class itself.
63

63

00:02:51,930  -->  00:02:54,510
Let me show you how this works.
64

64

00:02:54,510  -->  00:02:57,380
Called class_method of {cls}.
65

65

00:02:57,380  -->  00:02:59,740
I'll just print the parameter there,
66

66

00:02:59,740  -->  00:03:02,130
and then, because it is a class method,
67

67

00:03:02,130  -->  00:03:03,370
it's not an instance method,
68

68

00:03:03,370  -->  00:03:05,480
you no longer need an instance.
69

69

00:03:05,480  -->  00:03:06,800
That means that you can just do,
70

70

00:03:06,800  -->  00:03:10,960
ClassTest.class_method, and that's it,
71

71

00:03:10,960  -->  00:03:13,040
you don't have to pass in anything in there
72

72

00:03:13,040  -->  00:03:16,080
because we no longer need the object.
73

73

00:03:16,080  -->  00:03:17,980
Python is smart enough that when you do
74

74

00:03:17,980  -->  00:03:21,820
ClassTest.class_method and you don't pass anything in there,
75

75

00:03:21,820  -->  00:03:25,940
Python will actually pass ClassTest in there.
76

76

00:03:25,940  -->  00:03:29,920
So, actually cls is ClassTest,
77

77

00:03:29,920  -->  00:03:32,443
because that is what you called the method on.
78

78

00:03:33,520  -->  00:03:34,620
That is how this works.
79

79

00:03:34,620  -->  00:03:36,650
When you do ClassTest.class_method,
80

80

00:03:36,650  -->  00:03:40,040
given that you have put this @classmethod on top.
81

81

00:03:40,040  -->  00:03:41,710
We're going to learn what this is later on.
82

82

00:03:41,710  -->  00:03:43,450
Don't worry about it too much.
83

83

00:03:43,450  -->  00:03:47,600
Then Python will pass in the class as the argument
84

84

00:03:47,600  -->  00:03:48,793
to this parameter.
85

85

00:03:49,740  -->  00:03:52,110
If we save and play,
86

86

00:03:52,110  -->  00:03:53,910
you'll see that now you get that you've called
87

87

00:03:53,910  -->  00:03:58,120
the class_method of this class called ClassTest.
88

88

00:03:58,120  -->  00:04:00,760
Don't worry too much about the __main__,
89

89

00:04:00,760  -->  00:04:03,370
we will learn about that later on if it becomes relevant.
90

90

00:04:03,370  -->  00:04:06,160
It's largely irrelevant to most Python programmes.
91

91

00:04:06,160  -->  00:04:08,430
This just means that you've called the class_method
92

92

00:04:08,430  -->  00:04:09,690
of this class.
93

93

00:04:09,690  -->  00:04:11,850
We're going to find used cases for class_methods
94

94

00:04:11,850  -->  00:04:14,220
in just a little bit later on in this video.
95

95

00:04:14,220  -->  00:04:17,510
Finally, the last option is the @staticmethod.
96

96

00:04:18,426  -->  00:04:21,804
And this defines a static method that I'm going to call
97

97

00:04:21,804  -->  00:04:22,760
static_method.
98

98

00:04:22,760  -->  00:04:24,340
Again, you can call it whatever you want
99

99

00:04:24,340  -->  00:04:28,580
and now I'm not going to put anything inside the brackets.
100

100

00:04:28,580  -->  00:04:31,993
Here, we're going to do "Called static_method."
101

101

00:04:32,950  -->  00:04:35,880
In a static method, that is a method
102

102

00:04:35,880  -->  00:04:39,370
that doesn't have a parameter cls or self,
103

103

00:04:39,370  -->  00:04:42,370
and that has @staticmethod on top.
104

104

00:04:42,370  -->  00:04:45,710
These methods don't get anything when you call them.
105

105

00:04:45,710  -->  00:04:48,210
The class method gets the class normally
106

106

00:04:48,210  -->  00:04:50,090
and you can print it out or use it.
107

107

00:04:50,090  -->  00:04:52,900
The instance_methods gets the instance or the object
108

108

00:04:52,900  -->  00:04:55,360
and you can print it out or use it in them.
109

109

00:04:55,360  -->  00:04:58,560
But static methods don't get either of those things.
110

110

00:04:58,560  -->  00:05:00,710
And you can call them in the same way
111

111

00:05:00,710  -->  00:05:03,720
just by doing static_method inside ClassTest
112

112

00:05:03,720  -->  00:05:07,330
or ClassTest.static_method but Python will not put
113

113

00:05:07,330  -->  00:05:09,040
the ClassTest in the brackets.
114

114

00:05:09,040  -->  00:05:11,140
It will just call that method.
115

115

00:05:11,140  -->  00:05:13,480
This really isn't a method.
116

116

00:05:13,480  -->  00:05:16,710
It's just a function that you've placed inside a class.
117

117

00:05:16,710  -->  00:05:18,840
We should still call it a method but it doesn't have
118

118

00:05:18,840  -->  00:05:21,630
any information about the class or the object
119

119

00:05:21,630  -->  00:05:24,560
so it's really just its own separate function
120

120

00:05:24,560  -->  00:05:26,800
that lives inside the class.
121

121

00:05:26,800  -->  00:05:29,400
If you want a function inside the class
122

122

00:05:29,400  -->  00:05:31,410
that doesn't use the class for anything,
123

123

00:05:31,410  -->  00:05:35,630
or the instance, you can decorate it with @staticmethod.
124

124

00:05:35,630  -->  00:05:38,600
If you want a method that uses the class for something,
125

125

00:05:38,600  -->  00:05:41,500
then you can decorate it @classmethod.
126

126

00:05:41,500  -->  00:05:42,960
And if you want to use the object,
127

127

00:05:42,960  -->  00:05:45,820
or if you wanna create any other type of instance_method,
128

128

00:05:45,820  -->  00:05:47,940
then just don't put anything on top.
129

129

00:05:47,940  -->  00:05:50,240
No @classmethod or @staticmethod
130

130

00:05:50,240  -->  00:05:52,610
and make sure to include your parameter there.
131

131

00:05:52,610  -->  00:05:55,010
Again, if you call this like that and you press play,
132

132

00:05:55,010  -->  00:05:57,720
you just get Called static_method.
133

133

00:05:57,720  -->  00:05:59,683
So, what are these used for?
134

134

00:06:00,530  -->  00:06:02,880
Instance methods are used for most things.
135

135

00:06:02,880  -->  00:06:05,800
When you wanna produce an action that uses the data
136

136

00:06:05,800  -->  00:06:08,340
inside the object that you created earlier on for example,
137

137

00:06:08,340  -->  00:06:11,230
that is when instance methods would get used.
138

138

00:06:11,230  -->  00:06:13,620
Also, if you wanna call a method to modify
139

139

00:06:13,620  -->  00:06:16,110
some sort of data inside self or the object,
140

140

00:06:16,110  -->  00:06:17,840
then you would also use an instance method.
141

141

00:06:17,840  -->  00:06:21,930
Class methods are used often as factories
142

142

00:06:21,930  -->  00:06:24,130
and I'm going to show you what that is in a second.
143

143

00:06:24,130  -->  00:06:27,570
And finally, static_methods are used to just place a method
144

144

00:06:27,570  -->  00:06:28,670
inside a class.
145

145

00:06:28,670  -->  00:06:31,270
Because you feel like it belongs there for some reason.
146

146

00:06:31,270  -->  00:06:33,330
For you as a developer, you wanna put that method in there
147

147

00:06:33,330  -->  00:06:36,920
because it makes sense logically for code organisation
148

148

00:06:36,920  -->  00:06:39,410
or something like that, then you can use that.
149

149

00:06:39,410  -->  00:06:41,040
I'll say though that most of the time,
150

150

00:06:41,040  -->  00:06:43,610
you'll be using class methods and instance methods.
151

151

00:06:43,610  -->  00:06:46,010
You won't be using static methods all that much.
152

152

00:06:46,900  -->  00:06:49,570
Let's start with our factory example
153

153

00:06:49,570  -->  00:06:52,440
that is going to show you how you might use a class method.
154

154

00:06:52,440  -->  00:06:54,610
This example is gonna be a little bit tricky
155

155

00:06:54,610  -->  00:06:56,010
so please bear with me for a second
156

156

00:06:56,010  -->  00:06:58,930
while I explain first what I've done here.
157

157

00:06:58,930  -->  00:07:01,290
In a class, as well as methods,
158

158

00:07:01,290  -->  00:07:03,670
you can also put variables
159

159

00:07:03,670  -->  00:07:06,610
and these become class properties.
160

160

00:07:06,610  -->  00:07:10,270
If you wanted to print out this tuple here,
161

161

00:07:10,270  -->  00:07:13,680
all you have to do is access the TYPES tuple
162

162

00:07:13,680  -->  00:07:15,660
inside the Book class.
163

163

00:07:15,660  -->  00:07:18,690
You would do Book.TYPES,
164

164

00:07:18,690  -->  00:07:20,930
and that is how you access this tuple there.
165

165

00:07:20,930  -->  00:07:22,090
You can print it out
166

166

00:07:22,090  -->  00:07:24,793
and you can see that you get hardcover and paperback.
167

167

00:07:25,750  -->  00:07:29,130
We have some data stored inside the class,
168

168

00:07:29,130  -->  00:07:30,400
which is this tuple here,
169

169

00:07:30,400  -->  00:07:33,290
and a reason why you might put some data inside a class,
170

170

00:07:33,290  -->  00:07:35,650
as opposed to putting it outside the class,
171

171

00:07:35,650  -->  00:07:37,650
is because you feel like it belongs there
172

172

00:07:37,650  -->  00:07:40,030
or only the Book class is going to use it,
173

173

00:07:40,030  -->  00:07:41,820
or maybe there is some sort of reason,
174

174

00:07:41,820  -->  00:07:44,410
logical reason to put that data in there.
175

175

00:07:44,410  -->  00:07:46,950
Here I'm creating a class that talks about Books.
176

176

00:07:46,950  -->  00:07:50,053
So, it makes sense to have book types inside there.
177

177

00:07:51,040  -->  00:07:53,690
Then we're going to create an init method for this class.
178

178

00:07:53,690  -->  00:07:55,660
Remember, because this is an instance method
179

179

00:07:55,660  -->  00:07:57,920
it creates an instance and gives it back to you,
180

180

00:07:57,920  -->  00:07:59,720
you need the self parameter there.
181

181

00:07:59,720  -->  00:08:01,830
You can call self whatever you want, once again,
182

182

00:08:01,830  -->  00:08:04,130
but the convention in Python is to call it self.
183

183

00:08:04,130  -->  00:08:06,610
And then we're going to be creating objects
184

184

00:08:06,610  -->  00:08:09,280
of type Book that have three things.
185

185

00:08:09,280  -->  00:08:12,570
A name, a book type, and a weight.
186

186

00:08:12,570  -->  00:08:14,850
So, we will do self.name = name,
187

187

00:08:14,850  -->  00:08:16,183
Self.book_type = book_type,
188

188

00:08:17,610  -->  00:08:19,470
and self.weight = weight.
189

189

00:08:19,470  -->  00:08:21,460
This is a very common thing in Python
190

190

00:08:21,460  -->  00:08:23,640
to create an object with three different values
191

191

00:08:23,640  -->  00:08:26,280
that you then store each one inside a property
192

192

00:08:26,280  -->  00:08:27,970
of the object itself.
193

193

00:08:27,970  -->  00:08:30,070
Just to recap, if you were to create a variable
194

194

00:08:30,070  -->  00:08:32,260
such as Book, and make it an object,
195

195

00:08:32,260  -->  00:08:35,410
then you will need to pass in some information,
196

196

00:08:35,410  -->  00:08:39,230
let's say, "Harry Potter", "hardcover", and 1500,
197

197

00:08:39,230  -->  00:08:42,200
then you can print book.name
198

198

00:08:42,200  -->  00:08:43,640
and what that's going to do is it's going
199

199

00:08:43,640  -->  00:08:47,210
to pass in "Harry Potter" as the value for this parameter.
200

200

00:08:47,210  -->  00:08:50,040
Then that is going to be assigned to the name property
201

201

00:08:50,040  -->  00:08:52,400
of the self object, which is essentially
202

202

00:08:52,400  -->  00:08:54,730
an empty container at this stage,
203

203

00:08:54,730  -->  00:08:56,810
and then we're going to print it out down here.
204

204

00:08:56,810  -->  00:08:58,340
So if we press play,
205

205

00:08:58,340  -->  00:08:59,820
you'll see that "Harry Potter" comes out.
206

206

00:08:59,820  -->  00:09:01,560
That's how the data is flowing through
207

207

00:09:01,560  -->  00:09:03,730
this system at this stage.
208

208

00:09:03,730  -->  00:09:05,990
Then I'm also going to create a repr method
209

209

00:09:05,990  -->  00:09:07,040
so that it's a little bit easier
210

210

00:09:07,040  -->  00:09:08,660
to work with these books.
211

211

00:09:08,660  -->  00:09:12,430
And we are going to return a Book with this name.
212

212

00:09:12,430  -->  00:09:13,800
It is this Book type
213

213

00:09:13,800  -->  00:09:18,590
and it's weighing {self.weight}g&gt;.
214

214

00:09:18,590  -->  00:09:22,170
This is a representation of the book that would allow us,
215

215

00:09:22,170  -->  00:09:25,050
with all the data included inside,
216

216

00:09:25,050  -->  00:09:27,440
to recreate a book object if we wanted to.
217

217

00:09:27,440  -->  00:09:29,920
Remember, that is the purpose of the repr method.
218

218

00:09:29,920  -->  00:09:32,090
If you wanted to just print the book out nicely
219

219

00:09:32,090  -->  00:09:33,420
for users to read,
220

220

00:09:33,420  -->  00:09:36,500
then you could use the str method instead.
221

221

00:09:36,500  -->  00:09:37,930
Now instead of putting in book.name,
222

222

00:09:37,930  -->  00:09:40,780
we're just going to print book and I will press play
223

223

00:09:40,780  -->  00:09:42,390
and show you what the output is.
224

224

00:09:42,390  -->  00:09:43,747
Here's the book, "Harry Potter."
225

225

00:09:43,747  -->  00:09:47,190
It's a hardcover, weighing 1500 grammes.
226

226

00:09:47,190  -->  00:09:51,030
Now we come into the factories.
227

227

00:09:51,030  -->  00:09:52,810
Here's where we're gonna use class_methods.
228

228

00:09:52,810  -->  00:09:56,660
I wanna avoid, when I'm creating a new book object,
229

229

00:09:56,660  -->  00:09:58,400
when I'm calling the init method,
230

230

00:09:58,400  -->  00:10:01,910
I want to avoid passing in this "hardcover" string
231

231

00:10:01,910  -->  00:10:04,290
because I only want to be able to create books
232

232

00:10:04,290  -->  00:10:06,760
that are either hardcover or paperback,
233

233

00:10:06,760  -->  00:10:08,810
I wanna make sure that when I create a book,
234

234

00:10:08,810  -->  00:10:11,220
I use one of these two types.
235

235

00:10:11,220  -->  00:10:15,090
Right now, I can pass in comic book if I wanted to here
236

236

00:10:15,090  -->  00:10:17,840
and that would be totally fine because again,
237

237

00:10:17,840  -->  00:10:20,990
the data, this string here, becomes the value of book_type.
238

238

00:10:20,990  -->  00:10:23,470
Book_type gets assigned to self.book_type,
239

239

00:10:23,470  -->  00:10:24,580
and that gets used down here.
240

240

00:10:24,580  -->  00:10:26,770
There is no check for this,
241

241

00:10:26,770  -->  00:10:28,703
nothing is using this at the moment.
242

242

00:10:29,610  -->  00:10:32,920
What I'm going to do is I'm going to create a method
243

243

00:10:32,920  -->  00:10:36,360
that will take in the name and the weight
244

244

00:10:36,360  -->  00:10:40,340
and will create a new book object of type hardcover.
245

245

00:10:40,340  -->  00:10:44,063
I'm going to call this @classmethod hardcover,
246

246

00:10:45,450  -->  00:10:46,940
and again, because it is a class method,
247

247

00:10:46,940  -->  00:10:49,170
it takes in the class as the first parameter,
248

248

00:10:49,170  -->  00:10:51,080
usually called that cls in Python.
249

249

00:10:51,080  -->  00:10:53,220
And that is going to take in the name
250

250

00:10:53,220  -->  00:10:55,370
and the page weight,
251

251

00:10:55,370  -->  00:10:59,500
and what it's going to do is it is going to return a book
252

252

00:10:59,500  -->  00:11:02,310
with the name as the first argument,
253

253

00:11:02,310  -->  00:11:04,030
which then goes into the init method.
254

254

00:11:04,030  -->  00:11:09,030
Book.TYPES[0] as the second argument.
255

255

00:11:09,680  -->  00:11:12,180
Remember, Book.TYPES gives us this tuple.
256

256

00:11:12,180  -->  00:11:15,700
Book.TYPES[0] gives us the hardcover.
257

257

00:11:15,700  -->  00:11:18,957
And finally, the page_weight + 100
258

258

00:11:19,901  -->  00:11:21,940
for the weight of the book.
259

259

00:11:21,940  -->  00:11:23,690
I'm adding 100 because it's a hardcover
260

260

00:11:23,690  -->  00:11:25,290
so it's probably going to be a little heavier
261

261

00:11:25,290  -->  00:11:26,810
than a paperback.
262

262

00:11:26,810  -->  00:11:27,870
Hopefully this makes sense.
263

263

00:11:27,870  -->  00:11:30,710
It is a little bit confusing because you are using
264

264

00:11:30,710  -->  00:11:35,080
the class inside a method defined inside the class.
265

265

00:11:35,080  -->  00:11:36,750
Which I know is a little bit weird,
266

266

00:11:36,750  -->  00:11:40,700
but that is how you can create a new object
267

267

00:11:40,700  -->  00:11:42,233
inside of a class.
268

268

00:11:42,233  -->  00:11:44,630
This is something you can totally do.
269

269

00:11:44,630  -->  00:11:46,060
And because it is a class method,
270

270

00:11:46,060  -->  00:11:48,860
you no longer need to create your own object first.
271

271

00:11:48,860  -->  00:11:49,860
All you have to do now is say,
272

272

00:11:49,860  -->  00:11:53,640
Book.hardcover and pass in here "Harry Potter"
273

273

00:11:53,640  -->  00:11:56,370
and the page weight which was 1500.
274

274

00:11:56,370  -->  00:11:59,650
Notice that this hardcover method takes in the name
275

275

00:11:59,650  -->  00:12:02,360
and the page weight but it does not take in a type.
276

276

00:12:02,360  -->  00:12:04,730
The type is added inside the method,
277

277

00:12:04,730  -->  00:12:07,520
you don't have to pass it in as an argument.
278

278

00:12:07,520  -->  00:12:09,240
If I save and run that,
279

279

00:12:09,240  -->  00:12:11,130
you can see that now you get a book "Harry Potter,"
280

280

00:12:11,130  -->  00:12:13,770
which is a hardcover, and weighs 1600 grammes.
281

281

00:12:13,770  -->  00:12:17,230
Let's do the same thing for the paperback.
282

282

00:12:17,230  -->  00:12:18,860
Only now instead of Book.TYPES[0],
283

283

00:12:18,860  -->  00:12:20,330
I'm going to do Book.TYPES[1]
284

284

00:12:20,330  -->  00:12:22,853
and I'm going to remove this + 100.
285

285

00:12:23,740  -->  00:12:26,070
So now, if we wanted to create a softcover book,
286

286

00:12:26,070  -->  00:12:27,630
or a paperback, you can say something like,
287

287

00:12:27,630  -->  00:12:30,147
light = Book.paperback("Python 101"),
288

288

00:12:31,220  -->  00:12:33,360
and you can say for example, 600.
289

289

00:12:33,360  -->  00:12:35,890
If you then print the light book as well,
290

290

00:12:35,890  -->  00:12:38,460
you'll see that you get two books printed out now.
291

291

00:12:38,460  -->  00:12:41,290
Book "Harry Potter," which is hardcover, weighing 1600 grammes
292

292

00:12:41,290  -->  00:12:43,440
and Book "Python 101," which is a paperback,
293

293

00:12:43,440  -->  00:12:45,190
weighing 600 grammes.
294

294

00:12:45,190  -->  00:12:49,390
This is a very, very common way of using class methods
295

295

00:12:49,390  -->  00:12:51,700
because you have access to the class itself
296

296

00:12:51,700  -->  00:12:54,790
inside the method, that means that it is a perfect place
297

297

00:12:54,790  -->  00:12:59,500
to be creating new objects by using that class.
298

298

00:12:59,500  -->  00:13:01,850
With that said though, notice that I'm using Book here
299

299

00:13:01,850  -->  00:13:03,810
and I'm not using cls anywhere.
300

300

00:13:03,810  -->  00:13:07,580
Book is the class as we saw earlier on.
301

301

00:13:07,580  -->  00:13:10,300
And cls is also the class.
302

302

00:13:10,300  -->  00:13:11,950
They are interchangeable.
303

303

00:13:11,950  -->  00:13:15,590
Instead of Book, you can put cls in there.
304

304

00:13:15,590  -->  00:13:19,230
And instead of Book down here, you can put cls in there.
305

305

00:13:19,230  -->  00:13:20,700
You should do that.
306

306

00:13:20,700  -->  00:13:24,490
Use cls instead of Book and that will come in handy
307

307

00:13:24,490  -->  00:13:26,510
when you look at inheritance,
308

308

00:13:26,510  -->  00:13:28,330
which we will look at later on.
309

309

00:13:28,330  -->  00:13:30,420
Just because using cls gives you a little bit more
310

310

00:13:30,420  -->  00:13:32,650
flexibility down the line.
311

311

00:13:32,650  -->  00:13:35,670
Do use cls there since you've got access to it
312

312

00:13:35,670  -->  00:13:37,960
and if you were creating a static method
313

313

00:13:37,960  -->  00:13:39,050
instead of a class method,
314

314

00:13:39,050  -->  00:13:40,820
you would have to use book class
315

315

00:13:40,820  -->  00:13:42,680
because you wouldn't have cls.
316

316

00:13:42,680  -->  00:13:45,150
If you want to use cls use a class method,
317

317

00:13:45,150  -->  00:13:48,450
use the paramter, and you can create new objects
318

318

00:13:48,450  -->  00:13:50,810
and return them or use them however you like.
319

319

00:13:50,810  -->  00:13:52,460
Hopefully this makes sense.
320

320

00:13:52,460  -->  00:13:54,360
I know that it's a little bit confusing
321

321

00:13:54,360  -->  00:13:56,400
looking at class methods and static methods
322

322

00:13:56,400  -->  00:13:57,930
and it's been a bit of a long video
323

323

00:13:57,930  -->  00:13:59,630
but hopefully it was helpful.
324

324

00:13:59,630  -->  00:14:02,280
Hope you enjoyed it and I'll see you in the next one.
