13: def set_sex(self, sex): #Male, female, or unknown.
14: self.__sex = sex
15:
16: #Accessor methods for Bird data attributes
17: def get_feathers(self):
18: return self.__feathers
19:
20: def get_bones(self):
21: return self.__bones
22:
23: def get_eggs(self):
24: return self.__eggs
25:
26: def get_sex(self):
27: return self.__sex
28: pi@raspberrypi ~ $
Also notice in Listing 15.1 that there is one mutator method (lines 12 through 14) for the Bird class,
and there are four accessor methods (lines 16 through 27). There’s nothing too special here. Most of
these items should look similar to the class definitions in Hour 14.
Creating a Subclass
To add a subclass to the Bird base class, a barn swallow (also known as a European swallow) was
chosen. For simplicity’s sake, the BarnSwallow subclass is also overly simplified. Any
ornithologist will recognize that there is much more to a barn swallow than is listed here!
To add the BarnSwallow subclass, the subclass must be declared using the class declaration, as
shown here:
class BarnSwallow(Bird):
This class declaration allows you to define a subclass of BarnSwallow that inherits from its base
class (Bird). Thus, the BarnSwallow subclass object definition inherits all the data attributes and
methods from the Bird base class.
As with initializing a class, all the data attributes to be used in the BarnSwallow subclass are
initialized. This includes both base class and subclass data items, as shown here:
Click here to view code image
def __init__(self, feathers, bones, eggs, sex,
migratory, flock_size):
Within the initialization block, the init method of the Bird base class is used to initialize the
inherited data attributes feather, bones, eggs, and sex. This needs to be done for inheritance
purposes. You initialize the data attributes like this:
Click here to view code image
Bird.__init__(self, feathers, bones, eggs, sex)
Specialization of the BarnSwallow subclass can now begin. A barn swallow has the following
specialized data attributes. One data attribute is immutable (migratory) and one is mutable
(flock_size):
migratory—Set to yes because a barn swallow is known for its large migratory range.