Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
Python?
A. An “is a” relationship exists between a subclass and its base class. For example, a barn
swallow “is a” bird. A “has a” relationship exists between a class (a subclass or base class)
and one of its data attributes or methods. For example, looking at the class definition of a bird,
you can see from one of the data attribute statements that a bird “has a” feather.
Q. I added a subclass definition in a base class object file, and when I try to use one of the
stated methods, Python tells me the method is not found. Why?
A. Most likely, you do not have the correct indentation in the class object file. Try re-creating the
file within the IDLE 3 editor, which will give you some assistance creating the proper
indentation. An even better solution would be to put the subclass in its own object file, using
the IDLE 3 editor. Just be sure to include the proper import statements of the base class.
Q. Do I have to use subclasses?
A. No, there are no Python style police out there waiting to force you to use subclasses.
However, good form dictates that you should use subclasses to avoid the duplication issues in
the class problem.

Workshop


Quiz


1. A superclass is the same thing as a derived class. True or false?
2. What is it called when a subclass receives data attributes and methods from a base class?
3. Which Python statement correctly declares the subclass Ant of the base class Insect?
a. class Insect(Ant):
b. class Ant(Insect):
c. from Insect subclass Ant():

Answers


1. False. A superclass is also called a base class. A subclass, which inherits data attributes and
methods of a base class, is also called a derived class because some of its attributes and
methods are derived from the base class.
2. Inheritance is the term used when a subclass receives data attributes and methods from a base
class.
3. Answer b is correct. To properly create the subclass Ant from the base class Insect, you
use class Ant(Insect):.
Free download pdf