8000 Merge branch 'new-oop-exercise' · madamak/python-basics-exercises@6ba404b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ba404b

Browse files
committed
Merge branch 'new-oop-exercise'
2 parents 290ef39 + 4b28023 commit 6ba404b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

ch10-primer-on-oop/3-inherit-from-other-classes.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ def speak(self, sound="Bark"):
2525

2626

2727
# Exercise 2
28-
# Modified Dog class
29-
class Dog:
30-
species = "Canis familiaris"
28+
# Rectangle and Square classes
29+
class Rectangle:
30+
def __init__(self, length, width):
31+
self.length = length
32+
self.width = width
3133

32-
def __init__(self, name, age):
33-
self.name = name
34-
self.age = age
34+
def area(self):
35+
return self.length * self.width
3536

36-
def __str__(self):
37-
return f"{self.name} is {self.age} years old"
3837

39-
def speak(self, sound):
40-
if not isinstance(sound, str):
41-
raise TypeError("sound should be a string")
42-
return f"{self.name} says {sound}"
38+
class Square(Rectangle):
39+
def __init__(self, side_length):
40+
super().__init__(side_length, side_length)
41+
42+
square = Square(4)
43+
print(square.area())

0 commit comments

Comments
 (0)
0