-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-basic-abstract-classes.py
More file actions
318 lines (201 loc) · 7.46 KB
/
01-basic-abstract-classes.py
File metadata and controls
318 lines (201 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""Question: Create an abstract base class Shape with abstract methods area() and perimeter().
Implement concrete subclasses Circle and Rectangle that provide implementations for these methods.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What is the ABC module and how do you import it?
# - How do you create an abstract base class?
# - What is the @abstractmethod decorator?
# - How do concrete classes implement abstract methods?
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Import the ABC module
# ===============================================================================
# Explanation:
# The ABC (Abstract Base Class) module provides the infrastructure for defining
# abstract base classes in Python. We need to import ABC and abstractmethod.
from abc import ABC, abstractmethod
# What we accomplished in this step:
# - Imported the necessary components for creating abstract classes
# Step 2: Create the abstract Shape class
# ===============================================================================
# Explanation:
# An abstract base class inherits from ABC and contains one or more abstract methods.
# Abstract methods are declared but not implemented in the base class.
from abc import ABC, abstractmethod
class Shape(ABC):
pass # We'll add methods next
# What we accomplished in this step:
# - Created the abstract Shape class that inherits from ABC
# Step 3: Add abstract methods to Shape
# ===============================================================================
# Explanation:
# Abstract methods are decorated with @abstractmethod and typically contain
# only a pass statement or raise NotImplementedError.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
# What we accomplished in this step:
# - Added abstract area() and perimeter() methods
# - Used @abstractmethod decorator to mark them as abstract
# Step 4: Create the Circle subclass
# ===============================================================================
# Explanation:
# Concrete subclasses must implement all abstract methods from their parent class.
# The Circle class needs a radius and formulas for area and perimeter.
from abc import ABC, abstractmethod
import math
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
def perimeter(self):
return 2 * math.pi * self.radius
# What we accomplished in this step:
# - Created Circle class that inherits from Shape
# - Implemented both abstract methods with circle formulas
# Step 5: Create the Rectangle subclass
# ===============================================================================
# Explanation:
# The Rectangle class also needs to implement the abstract methods,
# but with formulas appropriate for rectangles.
from abc import ABC, abstractmethod
import math
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
def perimeter(self):
return 2 * math.pi * self.radius
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
# What we accomplished in this step:
# - Created Rectangle class that inherits from Shape
# - Implemented both abstract methods with rectangle formulas
# Step 6: Test our abstract classes
# ===============================================================================
# Explanation:
# Let's create instances and test our implementation. Note that we cannot
# instantiate the abstract Shape class directly.
from abc import ABC, abstractmethod
import math
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
def perimeter(self):
return 2 * math.pi * self.radius
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
# Test our classes:
circle = Circle(5)
rectangle = Rectangle(4, 6)
print(f"Circle with radius 5:")
print(f" Area: {circle.area():.2f}")
print(f" Perimeter: {circle.perimeter():.2f}")
print(f"\nRectangle 4x6:")
print(f" Area: {rectangle.area()}")
print(f" Perimeter: {rectangle.perimeter()}")
# Demonstrate polymorphism:
shapes = [Circle(3), Rectangle(5, 8), Circle(2)]
print(f"\nCalculating areas for multiple shapes:")
for i, shape in enumerate(shapes):
print(f" Shape {i+1} area: {shape.area():.2f}")
# Try to create abstract class instance (this will fail):
try:
shape = Shape()
except TypeError as e:
print(f"\nCannot instantiate abstract class: {e}")
# What we accomplished in this step:
# - Created and tested concrete instances
# - Demonstrated polymorphism with different shapes
# - Showed that abstract classes cannot be instantiated
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Abstract base classes using ABC
# - The @abstractmethod decorator
# - Implementing abstract methods in concrete subclasses
# - Polymorphism with abstract classes
# - Why abstract classes cannot be instantiated
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding a Triangle class!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================