Spending your inheritance
Darren Yates shows how Python tackles the complex but valuable concepts of inheritance and polymorphism.
They’re no doubt two of the more difficult programming concepts to get your head around, but inheritance and polymorphism are integral to maximising the value you get from using classes. They help simplify your code, but also give you the option of customising it at the same time to meet the specific needs of your programming tasks.
Python not only supports these features, but does so in a way that’s easy to implement and understand.
CLASSES OVERVIEW
These new concepts build on the idea of ‘classes’ that we looked at last month. Just to review quickly, a class is a code block that describes an object and how it works through data attributes (or variables) and functions. What’s more, a class is typically used like a ‘cookie-cutter’ to quickly create new copies. We looked at the idea of a smartphone being an object with common features, from screen size, operating system, RAM, flash storage, CPU and so on. But rather than creating a new set of variables and functions for every smartphone object we want to create, we create a ‘smartphone’ class and use it like a cookie-cutter to stamp out class copies or ‘instances’ of that ‘smartphone’ class. For example, you don’t need separate variable definitions to describe a Motorola Moto G5 or a Samsung Galaxy S8 — you just create two instances of the ‘smartphone’ class and those variables (and whatever functions the class has) are generated automatically, then populated by your code with the attributes of each phone.
Two of the many benefits of doing this are that it, first, helps you standardise on the data attributes you need to define a smartphone, but also, second, it helps reduce the opportunity for introducing errors, by standardising those attributes with a constant name and type — not to mention writing less code, which is always a good thing.
CLAIMING YOUR INHERITANCE
Inheritance takes building classes a step further. Smartphones are pretty cool, but they’re also specialised versions of computers, so you could say ‘a smartphone is a computer’ (something we’ve been saying for years!). This ‘is a’ relationship between objects, where the smartphone is a more specific version of a computer, can be used in creating classes. Instead of building separate classes for ‘smartphone’ and ‘computer’, the fact that a smartphone is a type of computer means that we can create a parent or ‘super’-class called ‘computer’ and derive a child or ‘sub’-class called ‘smartphone’ from the computer superclass. The important part of this is that the smartphone subclass gains or ‘inherits’ all of the attributes and functions of its computer superclass.
HOW IT WORKS
Grab this month’s Python source code pack from our website ( apcmag.com/ magstuff) and open up ‘computer.py’ in Python’s IDLE editor. (Don’t have Python? Grab the latest version 3 release for your preferred OS from www.python. org/downloads.) The file doesn’t do anything in particular, other than define the class called ‘computer’. The first function defined is the initialisation function, which expects a series of parameters ‘brand’, ‘model’, ‘cpu’ and so on (remember, the ‘self’ parameter refers to the class instance itself). Each of the parameters inside the brackets is then assigned to the appropriate attribute or internal variable (the double underscore ‘_ _’ ensures the variable can’t be accessed outside of the class boundary).
After that, the remaining functions are colloquially known as ‘setters’ (setting a class attribute value) and ‘getters’ (getting a class attribute value). So, basically, the ‘computer.py’ file defines the ‘Computer’ class.
Now open up ‘smartphone.py’. This one’s a bit more interesting — first up, we import the Computer class from the ‘computer.py’ file (provided the imported file is in the same filepath as your source code, referencing just the name of the file is fine). Next, we define a new class called ‘Smartphone’, however, inside the brackets is a reference to the ‘Computer’ class in the ‘computer.py’ file. This means we want to create a new subclass called ‘Smartphone’ based on the superclass ‘Computer’.
We follow this up with defining the initialisation function as we did with the Computer class, but this time, we include all the parameters required for the Computer class as well as the parameters