Last active
August 29, 2015 14:23
-
-
Save wagamama/1732a9875ac9e5a0c3ed to your computer and use it in GitHub Desktop.
Clean Code - Ch. 6
Data Shape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Square: | |
def __init__(self): | |
self.side = 0 | |
class Rectangle: | |
def __init__(self): | |
self.width = 0 | |
self.height = 0 | |
class Circle: | |
def __init__(self): | |
self.radius = 0 | |
class Geometry: | |
def __init__(self): | |
self.PI = 3.14159 | |
def area(self, shape): | |
if isinstance(shape, Square): | |
return shape.side ** 2 | |
elif isinstance(shape, Rectangle): | |
return shape.width * shape.height | |
elif isinstance(shape, Circle): | |
return self.PI * (shape.radius ** 2) | |
else: | |
raise Exception | |
def main(): | |
g = Geometry() | |
s = Square() | |
s.side = 5 | |
print g.area(s) | |
r = Rectangle() | |
r.width = 3 | |
r.height = 8 | |
print g.area(r) | |
c = Circle() | |
c.radius = 4 | |
print g.area(c) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment