Please send questions to
st10@humboldt.edu .
'''
lect09_6.py - contains two examples of Python classes
modified from "Learning Python", Lutz and Ascher, O'Reilly
p. 347, I think, but example is referred to in Chapters 19-23
adapted by: Sharon M. Tuttle
last modified: 10-24-06
'''
class Employee:
"""
Yup, this is the docstring for a class!
Class Employee: a worker
"""
def __init__(self, lastname, salary=0):
"""
Employee constructor
"""
self.lastname = lastname
self.salary = salary
def giveRaise(self, percent):
"""
Increase this employee's salary by <percent>
"""
self.salary = self.salary + (self.salary * percent)
def work(self):
"""
print a message saying what this Employee does
"""
print self.lastname, "does stuff"
def __repr__(self):
"""
return the string representation for this Employee
"""
return "<Employee: lastname=%s salary=$%.2f>" % (self.lastname, self.salary)
class Square:
"""
class Square: a shape with 4 equal sides, each of length
<side_length>, and a color <color>.
"""
def __init__(self, side_length, color="red"):
"""
Square constructor: if a color is not specified, the
square instance is assumed to be red
"""
self.side_length = side_length
self.color = color
def computeArea(self):
"""
Return the area of this square
"""
return self.side_length * self.side_length
def computeCircumf(self):
"""
Return the circumference of this square
"""
return self.side_length * 4
def __repr__(self):
"""
Return a string representation of this square
"""
return "<Square: side_length=%.1f color=%s>" % (self.side_length, self.color)