Skip to content

Commit e8bb94d

Browse files
committed
important fixes
1 parent ddd2149 commit e8bb94d

File tree

1 file changed

+58
-61
lines changed

1 file changed

+58
-61
lines changed

basics/classes.md

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ tkinter GUI's before I understood how they work. Everything I did with
55
classes worked, but I didn't understand how. Hopefully you'll first
66
learn to understand classes, and then learn to use them.
77

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-
128
## Why should I use custom classes in my projects?
139

1410
Python comes with a lot of classes that you are already familiar with.
@@ -89,34 +85,34 @@ names for your classes.
8985
Now we can make a Website instance by calling the class.
9086

9187
```python
92-
>>> stackoverflow = Website()
93-
>>> stackoverflow
88+
>>> github = Website()
89+
>>> github
9490
<__main__.Website object at 0x7f36e4c456d8>
95-
>>> type(stackoverflow)
91+
>>> type(github)
9692
<class '__main__.Website'>
9793
>>>
9894
```
9995

100-
We can say that `stackoverflow` is "a Website instance", "a Website
96+
We can say that `github` is "a Website instance", "a Website
10197
object" or "a Website". All of these mean the same thing.
10298

103-
Now we can attach more information about stackoverflow to our Website.
99+
Now we can attach more information about github to our Website.
104100

105101
```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
109105
>>>
110106
```
111107

112108
We can also access the information easily.
113109

114110
```python
115-
>>> stackoverflow.url
116-
'http://stackoverflow.com/'
117-
>>> stackoverflow.founding_year
111+
>>> github.url
112+
'https://github.com/'
113+
>>> github.founding_year
118114
2008
119-
>>> stackoverflow.free_to_use
115+
>>> github.free_to_use
120116
True
121117
>>>
122118
```
@@ -153,10 +149,10 @@ recommended to use it for code that needs to be reliable, but it's a
153149
handy way to see which attributes the instance contains.
154150

155151
```python
156-
>>> stackoverflow.__dict__
152+
>>> github.__dict__
157153
{'free_to_use': True,
158154
'founding_year': 2008,
159-
'url': 'http://stackoverflow.com/'}
155+
'url': 'https://github.com/'}
160156
>>> effbot.__dict__
161157
{}
162158
>>>
@@ -177,48 +173,49 @@ True
177173
Seems to be working, but what happened to the instances?
178174

179175
```python
180-
>>> stackoverflow.is_online
176+
>>> github.is_online
181177
True
182178
>>> effbot.is_online
183179
True
184180
>>>
185181
```
186182

187183
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!
189185

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
192188
the `Website` class.
193189

194190
```python
195-
>>> stackoverflow.__dict__
191+
>>> github.__dict__
196192
{'free_to_use': True,
197193
'founding_year': 2008,
198-
'url': 'http://stackoverflow.com/'}
194+
'url': 'https://github.com/'}
199195
>>> effbot.__dict__
200196
{}
201197
>>>
202198
```
203199

204200
`Website.is_online` is `Website`'s class attribute, and in Python you can
205201
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
207203
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`.
209205

210206
## Functions and methods
211207

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.
213210

214211
```python
215212
>>> def website_info(website):
216213
... print("URL:", website.url)
217214
... print("Founding year:", website.founding_year)
218215
... print("Free to use:", website.free_to_use)
219216
...
220-
>>> website_info(stackoverflow)
221-
URL: http://stackoverflow.com/
217+
>>> website_info(github)
218+
URL: https://github.com/
222219
Founding year: 2008
223220
Free to use: True
224221
>>>
@@ -230,49 +227,49 @@ Website class?
230227

231228
```python
232229
>>> Website.info = website_info
233-
>>> Website.info(stackoverflow)
234-
URL: http://stackoverflow.com/
230+
>>> Website.info(github)
231+
URL: https://github.com/
235232
Founding year: 2008
236233
Free to use: True
237234
>>>
238235
```
239236

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?
242239

243240
```python
244-
>>> stackoverflow.info()
245-
URL: http://stackoverflow.com/
241+
>>> github.info()
242+
URL: https://github.com/
246243
Founding year: 2008
247244
Free to use: True
248245
>>>
249246
```
250247

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
252249
magically worked!
253250

254-
`Website.info` is our `website_info` function, so `stackoverflow.info`
251+
`Website.info` is our `website_info` function, so `github.info`
255252
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()`!
257254

258-
But is `stackoverflow.info` the same thing as `Website.info`?
255+
But is `github.info` the same thing as `Website.info`?
259256

260257
```python
261258
>>> Website.info
262259
<function website_info at 0x7f36e4c39598>
263-
>>> stackoverflow.info
260+
>>> github.info
264261
<bound method website_info of <__main__.Website object at 0x7f36e4c456d8>>
265262
>>>
266263
```
267264

268265
It's not.
269266

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
271268
class attribute, the instances will have a method with the same name.
272269
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.
276273

277274
In other words, **`Class.method(instance)` does the same thing as
278275
`instance.method()`**. This also works with built-in classes, for
@@ -285,23 +282,23 @@ it later?
285282

286283
```python
287284
>>> class Website:
288-
... def info(self): # self will be stackoverflow
285+
... def info(self): # self will be github
289286
... print("URL:", self.url)
290287
... print("Founding year:", self.founding_year)
291288
... print("Free to use:", self.free_to_use)
292289
...
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/
299296
Founding year: 2008
300297
Free to use: True
301298
>>>
302299
```
303300

304-
It's working. The `self` argument in `Website.info` was `stackoverflow`.
301+
It's working. The `self` argument in `Website.info` was `github`.
305302
You could call it something else too such as `me`, `this` or `instance`,
306303
but use `self` instead. Other Python users have gotten used to it, and
307304
the official style guide recommens it also.
@@ -320,20 +317,20 @@ Maybe we could add a method to do that?
320317
... print("Founding year:", self.founding_year)
321318
... print("Free to use:", self.free_to_use)
322319
...
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/
327324
Founding year: 2008
328325
Free to use: True
329326
>>>
330327
```
331328

332329
That works. The attributes we defined in the initialize method are also
333330
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`.
335332

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
337334
a "magic" method that runs when we create a new Website by calling the
338335
Website class. It's called `__init__` and it does nothing by default. If
339336
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:
350347
... print("Founding year:", self.founding_year)
351348
... print("Free to use:", self.free_to_use)
352349
...
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/
356353
Founding year: 2008
357354
Free to use: True
358355
>>>

0 commit comments

Comments
 (0)