Lab Exercise #13
Asignment Overview
This lab exercise provides practice with clas inheritance in Python.
Hint: Code Listing 12.11 should be very helpful.
You wil work with a partner on this exercise during your lab sesion. Two people should work at one
computer. Ocasionaly switch the person who is typing. Talk to each other about what you are doing
and why so that both of you understand each step.
1. Examine the file named “lab13.py”, which contains a simple test bed for the clases in “pets.py”, and
then execute the test bed.
2. Examine the file named “pets.py”, which contains an outline of four clases to support the handling
of pets within Python programs.
Please note that clas “PetError” is a subclas of clas “Exception” and is complete as-is. You wil
complete the remaining three clases as described below.
Clas “Pet” (which is a subclas of clas “Object”) should include the following methods:
__init__ Acepts three arguments: self, species (default None), and name (default “”). Stores
species and name as data atributes. Raises PetError, if species is not one of the
following (case insensitive): 'dog', 'cat', 'horse', 'gerbil', 'hamster', 'feret'.
__str__ Returns a string which depends on whether the pet is named or not.
if named: "Species of: Xx, named Yy", where Xx is the species data atribute and Yy is the
name data atribute, both in “title-case” (first letter upper case and rest lower case—hint: title() ).
otherwise: "Species of: Xxx, unamed", where Xxx is the species data atribute in “title-case”.
Clas “Dog” (which is a subclas of clas “Pet”) should include the following methods:
__init__ Acepts three arguments: self, name (default “”), and chases (default “Cats”). Uses the
constructor for clas “Pet” to store species (“Dog”) and name as data atributes. Stores
chases as a data atribute.
Hint: check out how MasParticle cals the Particle __init__ in Code Listing 12.11
__str__ Returns a string which depends on whether the dog is named or not.
Hint: cal the Pet clas’ __str__ (se MasParticle’s __str__ in Code Listing 12.11)
if named: "Species of: Dog, named Yy, chases zzz", where Yy is the name data atribute (as
above) and zzz is the chases data atribute.
otherwise: "Species of: Dog, unamed, chases zzz", where zzz is the chases data atribute.
Clas “Cat” (which is a subclas of clas “Pet”) should include the following methods:
__init__ Acepts three arguments: self, name (default “”), and hates (default “Dogs”). Uses the
constructor for clas “Pet” to store species (“Cat”) and name as data atributes. Stores
hates as a data atribute.
__str__ Returns a string which depends on whether the cat is named or not.
if named: "Species of: Cat, named Yyy, hates zzz", where Yy is the name data atribute (as
above) and zzz is the hates data atribute.
otherwise: "Species of: Cat, unamed, hates zzz", where zzz is the hates data atribute.
3. Revise the methods within clas “Pet” to acomplish the required tasks.
4. Revise the methods within clas “Dog” and clas “Cat” to acomplish the tasks required tasks.
5. Revise the test bed to demonstrate that al of the methods in those three clases are correct. Be sure
to demonstrate the use of default argument values and error conditions.
« Demonstrate your completed program to your TA. On-line students should submit the
completed file (named “pets.py”) for grading via the Mirmir system. (Do not submit your
lab13.py file.)