-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircleOOP.py
101 lines (81 loc) · 3.45 KB
/
circleOOP.py
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
from abc import ABC, abstractmethod
from math import pi
# All classes must use a float method (?)
class Shape(ABC):
@abstractmethod
def area(self) -> float:
# Returns: float: area of the shape
pass
# Class for a circle defined by its radius
class Circle(Shape):
# Constructor for a circle defined by its radius
# Args: radius (float): radius of the circle
# Returns: None
# __ declares private characteristics (?)
def __init__(self, radius: float) -> None:
self.radius = radius
# Calculate the circumference of the circle
# Abstracts the circumference away from the user
def circumference(self):
return 2 * pi * self.radius
# Calculate the area of the circle
# Abstracts the area away from the user
# Polymorphic: area() is defined in Shape
def area(self) -> float:
return pi * self.radius ** 2
def __repr__(self) -> str:
# __repr__ is supposed to be unambiguous... eg. for logging
# If __str__ is not defined, __repr__ is used instead
# Returns: str: description of the circle
return f"Instance of {self.__class__} with radius {self.radius}"
def __str__(self) -> str:
# __str__ is supposed to be readable
# Returns: str: description of the circle
return f"A circle with a radius of {self.radius}, " \
f"a circumference of {self.circumference():.3f}, " \
f"and an area of {self.area():.3f}" # what is ():.3f?
def __eq__(self, __o: object): # what is __o?
# Define equality for two circles
# Args: __o (object): object to compare to
# Returns: bool: True if other circle has same radius
return isinstance(__o, self.__class__) and __o.radius == self.radius
# Class for a square defined by its radius (1/2 side)
class Square(Shape):
# Constructor for a square defined by its radius
# Args: radius (float): radius of the square
# Returns: None
# __ declares private characteristics (?)
def __init__(self, radius: float) -> None:
self.radius = radius
# Calculate the area of the circle
# Abstracts the area away from the user
# Polymorphic: area() is defined in Shape
def area(self) -> float:
return self.radius * 4
def __repr__(self) -> str:
# __repr__ is supposed to be unambiguous... eg. for logging
# If __str__ is not defined, __repr__ is used instead
# Returns: str: description of the square
return f"Instance of {self.__class__} with radius {self.radius}"
def __str__(self) -> str:
# __str__ is supposed to be readable
# Returns: str: description of the square
return f"A square with a radius of {self.radius}, " \
f"and an area of {self.area():.3f}" # what is ():.3f? Rounding?
def __eq__(self, __o: object): # what is __o?
# Define equality for two circles
# Args: __o (object): object to compare to
# Returns: bool: True if other circle has same radius
return isinstance(__o, self.__class__) and __o.radius == self.radius
# Tests
a = Circle(1)
b = Circle(2)
c = Circle(2)
shapes = [Circle(2), Square(3)]
print(a.__repr__())
print(a)
print(a == b) # Not true, radii 1 != 2
print(b == c) # Equality
print(b is c) # Identity
for shape in shapes:
print(f'{shape} has an area of {shape.area():.3f}')