-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path16.abstraction.py
More file actions
39 lines (28 loc) · 787 Bytes
/
16.abstraction.py
File metadata and controls
39 lines (28 loc) · 787 Bytes
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
from abc import ABC, abstractmethod
class Shape(ABC):
"""
Abstract base class.
Defines a strict contract for all shapes.
"""
@abstractmethod
def area(self) -> float:
"""Return the area of the shape."""
pass
@abstractmethod
def perimeter(self) -> float:
"""Return the perimeter of the shape."""
pass
class Rectangle(Shape):
"""
Concrete implementation of Shape.
"""
def __init__(self, width: float, height: float):
self.width = width
self.height = height
def area(self) -> float:
return self.width * self.height
def perimeter(self) -> float:
return 2 * (self.width + self.height)
shape = Rectangle(5, 10)
print(shape.area())
print(shape.perimeter())