""" Some simple examples of new style classes that motivate and introduce the use of properties. """ import math class Box(object): def __repr__(self): return "" % (self.length, self.width, self.area) class Box1(Box): """A rectangle""" def __init__(self,l=1,w=1): self.length = l self.width = w self.area = l * w class Box2(Box): """A rectangle with area getter""" def __init__(self,l=1,w=1): self.length = l self.width = w def get_area(self): return self.length * self.width def set_area(self, val=1): print "Warning: area is read only!" class Box3(Box): """A rectangle with area property""" def __init__(self,l=1,w=1): self.length = l self.width = w def get_area(self): return self.length * self.width def set_area(self, val=1): print "Warning: area is read only!" area = property(get_area, set_area) class Box4(Box): """A rectangle with area property""" def __init__(self,l=1,w=1): self.length = l self.width = w @property def area(self): return self.length * self.width @area.setter def area(self, val=1): print "Warning: area is read only!" class Box5(Box): """A rectangle with area property with setter""" def __init__(self,l=1,w=1): self.length = l self.width = w self._color = None @property def area(self): return self.length * self.width @area.setter def area(self, val=1): self.length = self.width = math.sqrt(val) @property def color(self): return self._color @color.setter def color(self, val): self._color = val @color.deleter def color(self): del self._color