@@ -5,10 +5,6 @@ tkinter GUI's before I understood how they work. Everything I did with
5
5
classes worked, but I didn't understand how. Hopefully you'll first
6
6
learn to understand classes, and then learn to use them.
7
7
8
- This tutorial assumes that you know [ how functions work] ( using-functions.md )
9
- and [ how to create your own functions] ( defining-functions.md ) . If you
10
- don't I highly recommend learning that first, and then moving to classes.
11
-
12
8
## Why should I use custom classes in my projects?
13
9
14
10
Python comes with a lot of classes that you are already familiar with.
@@ -89,34 +85,34 @@ names for your classes.
89
85
Now we can make a Website instance by calling the class.
90
86
91
87
``` python
92
- >> > stackoverflow = Website()
93
- >> > stackoverflow
88
+ >> > github = Website()
89
+ >> > github
94
90
< __main__.Website object at 0x 7f36e4c456d8>
95
- >> > type (stackoverflow )
91
+ >> > type (github )
96
92
< class ' __main__.Website' >
97
93
>> >
98
94
```
99
95
100
- We can say that ` stackoverflow ` is "a Website instance", "a Website
96
+ We can say that ` github ` is "a Website instance", "a Website
101
97
object" or "a Website". All of these mean the same thing.
102
98
103
- Now we can attach more information about stackoverflow to our Website.
99
+ Now we can attach more information about github to our Website.
104
100
105
101
``` python
106
- >> > stackoverflow .url = ' http ://stackoverflow .com/'
107
- >> > stackoverflow .founding_year = 2008
108
- >> > stackoverflow .free_to_use = True
102
+ >> > github .url = ' https ://github .com/'
103
+ >> > github .founding_year = 2008
104
+ >> > github .free_to_use = True
109
105
>> >
110
106
```
111
107
112
108
We can also access the information easily.
113
109
114
110
``` python
115
- >> > stackoverflow .url
116
- ' http ://stackoverflow .com/'
117
- >> > stackoverflow .founding_year
111
+ >> > github .url
112
+ ' https ://github .com/'
113
+ >> > github .founding_year
118
114
2008
119
- >> > stackoverflow .free_to_use
115
+ >> > github .free_to_use
120
116
True
121
117
>> >
122
118
```
@@ -153,10 +149,10 @@ recommended to use it for code that needs to be reliable, but it's a
153
149
handy way to see which attributes the instance contains.
154
150
155
151
``` python
156
- >> > stackoverflow .__dict__
152
+ >> > github .__dict__
157
153
{' free_to_use' : True ,
158
154
' founding_year' : 2008 ,
159
- ' url' : ' http ://stackoverflow .com/' }
155
+ ' url' : ' https ://github .com/' }
160
156
>> > effbot.__dict__
161
157
{}
162
158
>> >
@@ -177,48 +173,49 @@ True
177
173
Seems to be working, but what happened to the instances?
178
174
179
175
``` python
180
- >> > stackoverflow .is_online
176
+ >> > github .is_online
181
177
True
182
178
>> > effbot.is_online
183
179
True
184
180
>> >
185
181
```
186
182
187
183
What was that? Setting ` Website.is_online ` to a value also set
188
- ` stackoverflow .is_online` and ` effbot.is_online ` to that value!
184
+ ` github .is_online` and ` effbot.is_online ` to that value!
189
185
190
- Actually, ` is_online ` is still not in stackoverflow 's or effbot's
191
- ` __dict__ ` . stackoverflow and effbot get that attribute directly from
186
+ Actually, ` is_online ` is still not in github 's or effbot's
187
+ ` __dict__ ` . github and effbot get that attribute directly from
192
188
the ` Website ` class.
193
189
194
190
``` python
195
- >> > stackoverflow .__dict__
191
+ >> > github .__dict__
196
192
{' free_to_use' : True ,
197
193
' founding_year' : 2008 ,
198
- ' url' : ' http ://stackoverflow .com/' }
194
+ ' url' : ' https ://github .com/' }
199
195
>> > effbot.__dict__
200
196
{}
201
197
>> >
202
198
```
203
199
204
200
` Website.is_online ` is ` Website ` 's class attribute, and in Python you can
205
201
access class attributes through instances also, so in this case
206
- ` stackoverflow .is_online` points to ` Website.is_online ` . That can be
202
+ ` github .is_online` points to ` Website.is_online ` . That can be
207
203
confusing, which is why it's not recommended to use class attributes like
208
- this. Use instance attributes instead, e.g. ` stackoverflow .is_online = True` .
204
+ this. Use instance attributes instead, e.g. ` github .is_online = True` .
209
205
210
206
## Functions and methods
211
207
212
- Let's define a function that prints information about a website.
208
+ Let's [ define a function] ( defining-functions.md ) that prints information
209
+ about a website.
213
210
214
211
``` python
215
212
>> > def website_info (website ):
216
213
... print (" URL:" , website.url)
217
214
... print (" Founding year:" , website.founding_year)
218
215
... print (" Free to use:" , website.free_to_use)
219
216
...
220
- >> > website_info(stackoverflow )
221
- URL : http :// stackoverflow .com/
217
+ >> > website_info(github )
218
+ URL : https :// github .com/
222
219
Founding year: 2008
223
220
Free to use: True
224
221
>> >
@@ -230,49 +227,49 @@ Website class?
230
227
231
228
``` python
232
229
>> > Website.info = website_info
233
- >> > Website.info(stackoverflow )
234
- URL : http :// stackoverflow .com/
230
+ >> > Website.info(github )
231
+ URL : https :// github .com/
235
232
Founding year: 2008
236
233
Free to use: True
237
234
>> >
238
235
```
239
236
240
- It's working, but ` Website.info(stackoverflow ) ` is a lot of typing, so
241
- wouldn't ` stackoverflow .info()` be much better?
237
+ It's working, but ` Website.info(github ) ` is a lot of typing, so
238
+ wouldn't ` github .info()` be much better?
242
239
243
240
``` python
244
- >> > stackoverflow .info()
245
- URL : http :// stackoverflow .com/
241
+ >> > github .info()
242
+ URL : https :// github .com/
246
243
Founding year: 2008
247
244
Free to use: True
248
245
>> >
249
246
```
250
247
251
- What the heck happened? We didn't define a ` stackoverflow .info` , it just
248
+ What the heck happened? We didn't define a ` github .info` , it just
252
249
magically worked!
253
250
254
- ` Website.info ` is our ` website_info ` function, so ` stackoverflow .info`
251
+ ` Website.info ` is our ` website_info ` function, so ` github .info`
255
252
should also be the same function. But ` Website.info ` takes a ` website `
256
- argument, which we didn't give it when we called ` stackoverflow .info()` !
253
+ argument, which we didn't give it when we called ` github .info()` !
257
254
258
- But is ` stackoverflow .info` the same thing as ` Website.info ` ?
255
+ But is ` github .info` the same thing as ` Website.info ` ?
259
256
260
257
``` python
261
258
>> > Website.info
262
259
< function website_info at 0x 7f36e4c39598>
263
- >> > stackoverflow .info
260
+ >> > github .info
264
261
< bound method website_info of < __main__.Website object at 0x 7f36e4c456d8>>
265
262
>> >
266
263
```
267
264
268
265
It's not.
269
266
270
- Instead, ` stackoverflow .info` is a ** method** . If we set a function as a
267
+ Instead, ` github .info` is a ** method** . If we set a function as a
271
268
class attribute, the instances will have a method with the same name.
272
269
Methods are "links" to the class attribute functions. So
273
- ` Website.info(stackoverflow ) ` does the same thing as ` stackoverflow .info()` ,
274
- and when ` stackoverflow .info()` is called it automatically gets
275
- ` stackoverflow ` as an argument.
270
+ ` Website.info(github ) ` does the same thing as ` github .info()` ,
271
+ and when ` github .info()` is called it automatically gets
272
+ ` github ` as an argument.
276
273
277
274
In other words, ** ` Class.method(instance) ` does the same thing as
278
275
` instance.method() ` ** . This also works with built-in classes, for
@@ -285,23 +282,23 @@ it later?
285
282
286
283
``` python
287
284
>> > class Website :
288
- ... def info (self ): # self will be stackoverflow
285
+ ... def info (self ): # self will be github
289
286
... print (" URL:" , self .url)
290
287
... print (" Founding year:" , self .founding_year)
291
288
... print (" Free to use:" , self .free_to_use)
292
289
...
293
- >> > stackoverflow = Website()
294
- >> > stackoverflow .url = ' http ://stackoverflow .com/'
295
- >> > stackoverflow .founding_year = 2008
296
- >> > stackoverflow .free_to_use = True
297
- >> > stackoverflow .info()
298
- URL : http :// stackoverflow .com/
290
+ >> > github = Website()
291
+ >> > github .url = ' https ://github .com/'
292
+ >> > github .founding_year = 2008
293
+ >> > github .free_to_use = True
294
+ >> > github .info()
295
+ URL : https :// github .com/
299
296
Founding year: 2008
300
297
Free to use: True
301
298
>> >
302
299
```
303
300
304
- It's working. The ` self ` argument in ` Website.info ` was ` stackoverflow ` .
301
+ It's working. The ` self ` argument in ` Website.info ` was ` github ` .
305
302
You could call it something else too such as ` me ` , ` this ` or ` instance ` ,
306
303
but use ` self ` instead. Other Python users have gotten used to it, and
307
304
the official style guide recommens it also.
@@ -320,20 +317,20 @@ Maybe we could add a method to do that?
320
317
... print (" Founding year:" , self .founding_year)
321
318
... print (" Free to use:" , self .free_to_use)
322
319
...
323
- >> > stackoverflow = Website()
324
- >> > stackoverflow .initialize(' http ://stackoverflow .com/' , 2008 , True )
325
- >> > stackoverflow .info()
326
- URL : http :// stackoverflow .com/
320
+ >> > github = Website()
321
+ >> > github .initialize(' https ://github .com/' , 2008 , True )
322
+ >> > github .info()
323
+ URL : https :// github .com/
327
324
Founding year: 2008
328
325
Free to use: True
329
326
>> >
330
327
```
331
328
332
329
That works. The attributes we defined in the initialize method are also
333
330
available in the info method. We could also access them directly from
334
- ` stackoverflow ` , for example with ` stackoverflow .url` .
331
+ ` github ` , for example with ` github .url` .
335
332
336
- But we still need to call ` stackoverflow .initialize` . In Python, there's
333
+ But we still need to call ` github .initialize` . In Python, there's
337
334
a "magic" method that runs when we create a new Website by calling the
338
335
Website class. It's called ` __init__ ` and it does nothing by default. If
339
336
our ` __init__ ` method takes other arguments than self we can call the
@@ -350,9 +347,9 @@ class with arguments and they will be given to `__init__`. Like this:
350
347
... print (" Founding year:" , self .founding_year)
351
348
... print (" Free to use:" , self .free_to_use)
352
349
...
353
- >> > stackoverflow = Website(' http ://stackoverflow .com/' , 2008 , True )
354
- >> > stackoverflow .info()
355
- URL : http :// stackoverflow .com/
350
+ >> > github = Website(' https ://github .com/' , 2008 , True )
351
+ >> > github .info()
352
+ URL : https :// github .com/
356
353
Founding year: 2008
357
354
Free to use: True
358
355
>> >
0 commit comments