def perimeter(self): 'Circumference corrected for the rubber on the tire' return Circle.perimeter( self) * self.RUBBER_RATIO # Extending using a direct reference return super( Tire, self).perimeter() * self.RUBBER_RATIO # Extending using super() return 2.0 * math.pi * self.radius * self.RUBBER_RATIO # Overriding
print "seed with Jenny's number: {jenny}".format(jenny=jenny) circles = [Circle(random()) for i in xrange(n)] areas = [circle.area() for circle in circles] average_area = sum(areas) / n print 'The average area is %.5f' % average_area print ## Rubber Sheet Company #################################### cut_template = [0.1, 0.2, 0.7] print 'Specifications for the cut template', cut_template circles = [Circle(cut_radius) for cut_radius in cut_template] for i, c in enumerate(circles, start=1): print 'Circle #%d:' % i print 'A rubber circle with a cut radius of', c.radius print 'has a perimeter of', c.perimeter() print 'and a cold area of', c.area() c.radius *= 1.1 # c.set_radius(c.get_radius() * 1.1) print 'and a warm area of', c.area() print ## National Tire Company ################################## import math class Tire(Circle): 'Circle analytics on a rubber tire' RUBBER_RATIO = 1.25
m = MonsterTire(30) print 'A monster tire with an inner radius of', m.radius print 'has an inner area of', m.area() print 'and an outer perimeter of', m.perimeter() print ## National Trucking Company ############################################## print u'An inclinometer reading of 5\N{degree sign}', print 'is a %.1f%% grade.' % Circle.angle_to_grade(5) print ## National Graphics Company ############################################## c = Circle.from_bbd(25) print 'A circle with a bounding box diagonal of 25' print 'has a radius of', c.radius print 'a perimeter of', c.perimeter() print 'and an area of', c.area() print ## US Gov't ############################################################### # ISO 10666: No circle software shall compute an area directly from # instance data. It MUST first call perimeter and infer the data indirectly. # ISO 10667: No circle software shall store the radius in an instance. # It MUST store the diameter and ONLY the diameter.
def perimeter(self): return Circle.perimeter(self) * self.RUBBER_RATIO # Extending return 2.0 * 3.14 * self.radius * self.RUBBER_RATIO # Overriding
def perimeter(self): 'Odometer correct perimeter that accounts for the rubber on tire' # Overriding -- don't call the parent method # Extending -- call the parent method and then modify it's result return Circle.perimeter(self) * self.RUBBER_RATIO
print 'A tire with an inner radius of', t.radius print 'and inner area of', t.area() print 'and an outer perimeter of', t.perimeter() print ## National Trucking Company ##### print u'A 5\N{DEGREE SIGN} degree inclinometer reading', print 'is %.1f%% grade' % Circle.angle_to_grade(5) print ## National Graphic Company ######### c = Circle.from_bbd(40) print 'A circle with a bounding box diagonal of 40' print 'has a perimeter of', c.perimeter() print 'and a radius of', c.radius print 'and an area of', c.area() print ## US Government ########### # ISO 10111: All circle software SHALL NOT access the radius directly. # It MUST call perimeter() and infer the radius indirectly # ISO 10112: No circle software shall store the radius. It must store the diameter and only the diameter.
def perimeter(self): 'Perimeter adjusted for width of tire' return Circle.perimeter(self) * 1.25
def perimeter(self): return Circle.perimeter(self) * self.RUBBER_RATIO # Extending
def perimeter(self): "adjusted perimeter for width of tire" return Circle.perimeter(self) * 1.25