# looking at a simple Python class
# a class combines a number of methods/functions that belong together
class Box(object):
"""class names by convention are capitalized, (object) is an optional inheritance"""
def __init__(self, depth, height, width):
"""__init__() is called first (acts as Constructor)
brings in data from outside the class and makes it
usable within the class via the instance object self.
Look at self as the data carrier within the class.
"""
self.depth = depth
self.height = height
self.width = width
def volume(self):
return self.height * self.width * self.depth
def surface(self):
return 2*self.height*self.width + 2*self.height*self.depth + 2*self.width*self.depth
# construct an instance of a 10x10x10 box and reference it with box1
box1 = Box(10, 10 ,10)
print "A 10x10x10 box has a volume of", box1.volume()
print "and a surface area of", box1.surface()
print
print "Let's change the depth to 5"
# construct an instance of a 5x10x10 box and reference it with box2
box2 = Box(5, 10 ,10)
print "A 5x10x10 box has a volume of", box2.volume()
print "and a surface area of", box2.surface()
print
print "Optional tests for the inquisitive folks:"
print "box1 =", box1
print "box2 =", box2
print "The depth of box2 is", box2.depth
print
# Can we set the box dimensions directly? Let's try it.
box1.depth = 5
box1.height = 5
box1.width = 5
print "Box1 volume after setting box1 dimensions to 5x5x5 =", box1.volume()