I would change this part in an abstract class:
def getArea(self):
if (self.type == "Square"):
return self.width * self.height
elif (self.type == "Circle"):
return math.pi*(self.radius**2)
You can specify a default value in an abstract class and override the method in Rectangleor in Circle.
But you can get a better answer at https://codereview.stackexchange.com/ .
UPDATE (example):
from abc import ABCMeta, abstractmethod
class Shape:
__metaclass__ = ABCMeta
@abstractmethod
def getArea(self):
pass
class Rectangle (Shape):
def getArea(self):
return self.width * self.height
class Circle (Shape):
def getArea(self):
return math.pi*(self.radius**2)
UPDATE 2 (overload functions)
Ohad, python, init funtion:
def Shape:
def __init__(self, centrePoint, colour, **kwargs):
self.centrePoint = centrePoint
self.colour = colour
self.width = kwargs.get('width')
self.height = kwargs.get('height')
self.radius = kwargs.get('radius')
rect = Rectangle(0, "red", width=100, height=20)
circ = Circle(0, "blue", radius=5)
kwargs : , Python?
, :
>>> rect = Rectangle(...)
>>> print isinstance(rect, Rectangle)
True
>>> print isinstance(rect, Circle)
False