[Tutor] container class
Tim Wilson
wilson@isis.visi.com
Sat, 2 Feb 2002 22:20:17 -0600 (CST)
Hi everyone,
I've just put together a couple classes here investigating how to do a
container class. Does the following seem reasonable? There isn't yet
enough for any sort of useful program, but I think it shows my train of
thought. Any feedback appreciated.
-Tim
# PyAddress, a simple program to organize names and addresses
# by Tim Wilson <wilson@visi.com>
class AddressBook:
def __init__(self):
"""Initialize an empty address book."""
self.contents = {}
def addEntry(self, entry):
"""Add an entry to the address book.
Argument:
entry -- An AddressBookEntry instance
"""
self.contents[entry.ln + entry.fn] = entry
def printBook(self):
"""Print the contents of the address book."""
print "%-20s %-12s" % ('Name', 'Phone')
print '='*33
k = self.contents.keys()
k.sort()
for name in k:
entry = self.contents[name]
print entry
class AddressBookEntry:
def __init__(self, fn, ln, phone='', email=''):
"""Construct a new entry for the address book.
Arguments:
fn -- first name
ln -- last name
phone -- phone number
email -- email address
"""
self.fn = fn
self.ln = ln
self.phone = phone
self.email = email
def __str__(self):
"""One-line version of entry info."""
name = self.ln + ', ' + self.fn
return "%-20s %-12s" % (name, self.phone)
def details(self):
"""Print the full entry record."""
print self.fn, self.ln
print '=' * (len(self.fn) + len(self.ln) + 1)
print "Phone: %s" % self.phone
print "Email: %s" % self.email
--
Tim Wilson | Visit Sibley online: | Check out:
Henry Sibley HS | http://www.isd197.org | http://www.zope.com
W. St. Paul, MN | | http://slashdot.org
wilson@visi.com | <dtml-var pithy_quote> | http://linux.com