Skip to content

Instantly share code, notes, and snippets.

@ChristopherMacGown
Created August 27, 2011 03:38
Show Gist options
  • Save ChristopherMacGown/1174934 to your computer and use it in GitHub Desktop.
Save ChristopherMacGown/1174934 to your computer and use it in GitHub Desktop.
bj's homework
>>> import math
>>>
>>>
>>> class Shape(object):
... def area(self):
... raise NotImplementedError("An arbitrary shape doesn't have a defined "
... "area. Please use a descendent class.")
...
>>>
>>> class Circle(Shape):
... def __init__(self, radius):
... self.radius = radius
...
... def area(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
...
>>>
>>> class Square(Rectangle):
... def __init__(self, width):
... super(Square, self).__init__(width, width)
...
>>>
>>> Circle(10).area()
62.83185307179586
>>> Rectangle(10,20).area()
200
>>> Square(10).area()
100
>>> Rectangle(10,10).area()
100
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment