File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -215,3 +215,16 @@ def __str__(self):
215
215
print (cat0 .speak ())
216
216
print (cat1 .speak ())
217
217
218
+
219
+ class MagicalCat (Cat ):
220
+ catWord = "abracadabra"
221
+
222
+ def __init__ (self , name ):
223
+ super ().__init__ (name )
224
+ def speak (self ):
225
+ return f"{ self .name } says ✨'{ self .catWord } '✨"
226
+
227
+
228
+ cat2 = MagicalCat ("Mistoffelees" )
229
+ print (cat2 .speak ())
230
+
Original file line number Diff line number Diff line change @@ -308,7 +308,31 @@ Not so helpful! So we can add a `__str__ ` method that turns it into a more usef
308
308
return f"{self.name}, a {self.mood} cat"
309
309
```
310
310
311
+
311
312
```
312
313
Bustopher, a happy cat
313
314
Grizabella, a sad cat
314
315
```
316
+
317
+ You can also * inherit* from existing classes
318
+ ```
319
+
320
+ class MagicalCat(Cat):
321
+ catWord = "abracadabra"
322
+
323
+ def __init__(self, name):
324
+ super().__init__(name)
325
+ def speak(self):
326
+ return f"{self.name} says ✨'{self.catWord}'✨"
327
+
328
+
329
+ cat2 = MagicalCat("Mistoffelees")
330
+ print(cat2.speak())
331
+ ```
332
+
333
+ ```
334
+ Grizabella says 'meow'
335
+ Mistoffelees says ✨'abracadabra'✨
336
+ ```
337
+
338
+
You can’t perform that action at this time.
0 commit comments