By John Paul Mueller

As you might imagine, creating a fully functional, production-grade class (one that is used in a real-world Python application actually running on a system that is accessed by users) is time consuming because real classes perform a lot of tasks. Fortunately, Python supports a feature called inheritance.

By using inheritance, you can obtain the features you want from a parent class when creating a child class. Overriding the features that you don’t need and adding new features lets you create new classes relatively fast and with a lot less effort on your part. In addition, because the parent code is already tested, you don’t have to put quite as much effort into ensuring that your new class works as expected.

Building the child class

Parent classes are normally supersets of something. For example, you might create a parent class named Car and then create child classes of various car types around it. In this case, you build a parent class named Animal and use it to define a child class named Chicken. Of course, you can easily add other child classes after you have Animal in place, such as a Gorilla class. However, for this example, you build just the one parent and one child class. To use this class, you need to save it to disk. However, give your file the name BPPD_15_Animals.ipynb.

class Animal:

def __init__(self, Name="", Age=0, Type=""):

self.Name = Name

self.Age = Age

self.Type = Type

def GetName(self):

return self.Name

def SetName(self, Name):

self.Name = Name

def GetAge(self):

return self.Age

def SetAge(self, Age):

self.Age = Age

def GetType(self):

return self.Type

def SetType(self, Type):

self.Type = Type

def __str__(self):

return "{0} is a {1} aged {2}".format(self.Name,

self.Type,

self.Age)

class Chicken(Animal):

def __init__(self, Name="", Age=0):

self.Name = Name

self.Age = Age

self.Type = "Chicken"

def SetType(self, Type):

print("Sorry, {0} will always be a {1}"

.format(self.Name, self.Type))

def MakeSound(self):

print("{0} says Cluck, Cluck, Cluck!".format(self.Name))

The Animal class tracks three characteristics: Name, Age, and Type. A production application would probably track more characteristics, but these characteristics do everything needed for this example. The code also includes the required accessors for each of the characteristics. The __str__() method completes the picture by printing a simple message stating the animal characteristics.

The Chicken class inherits from the Animal class. Notice the use of Animal in parentheses after the Chicken class name. This addition tells Python that Chicken is a kind of Animal, something that will inherit the characteristics of Animal.

Notice that the Chicken constructor accepts only Name and Age. The user doesn’t have to supply a Type value because you already know that it’s a chicken. This new constructor overrides the Animal constructor. The three attributes are still in place, but Type is supplied directly in the Chicken constructor.

Someone might try something funny, such as setting her chicken up as a gorilla. With this in mind, the Chicken class also overrides the SetType() setter. If someone tries to change the Chicken type, that user gets a message rather than the attempted change. Normally, you handle this sort of problem by using an exception, but the message works better for this example by making the coding technique clearer.

Finally, the Chicken class adds a new feature, MakeSound(). Whenever someone wants to hear the sound a chicken makes, he can call MakeSound() to at least see it printed on the screen.