Select Page

Base class – 30-40 minutes

This can be a confusing topic if you don’t already have experience with object-oriented programming. It takes some reading, some thinking, maybe some re-reading, and the concepts will come to you. It’s not easy, but it’s definitely worth it. We’re going to create a base class that we are going to inherit from in our SSHHost class. We’re implementing inheritance.

You might wonder why we would go about this. Why break something that works so delightfully well?

There are some things that we need to do, like writing to a log, that are the same no matter what protocol we use to connect to host devices. By moving those capabilities to a base class we can reuse the code more easily. Capabilities in the base class will be available in the derived classes. So we can put a log function/method into the base class and reuse the code from any or all of the derived classes. Let’s see how that works.

Here’s a whole base class. Give it a quick look and then we’ll break it down.

We start out with a pretty standard class definition statement for HostDevice. Then we have a new concept:

We’re using Python’s built-in Abstract Base Class (ABC) infrastructure to tell Python that this class is an ABC. Say what? That just means that the base class is abstract and cannot be instantiated. That means you cannot create a HostDevice object directly.

How would Python know that HostDevice is an abstract class? We let Python know what’s up by using the abstractmethod decorator. For example:

What’s happening here is that we are creating the connect() method in HostDevice, but we are also specifying that the method is abstract. Because it is abstract any class that inherits from HostDevicemust implement a connect() method. Derived classes must implement abstract methods. Derived classes do not need to implement non-abstract methods, but may override them if appropriate.

When designing a family of classes we try to figure out the lowest common denominators. For example, we know that regardless of whether we use SSH or Telnet we will always need to connect to a host device. But how we connect is different based on whether we’re using SSH or Telnet. So we take the idea of connecting and represent it as the abstract base method connect().

In other cases, like log_filename_for_command, the code is always the same regardless of the protocol we use to connect to the host device. So we can implement the method in the base class and let it be shared by derived classes.

Now let’s take a look at an SSHHost class that is derived from our base class. Again, give it a read through and we’ll discuss on the other side.

Note that we are actually implementing the connect() method. The method has the code that is specific to an SSH implementation. In order to use SSHHost we have to implement each of the abstract methods.

We have mostly tried, first, to figure out the features we need. And, second, we decided on whether we can implement the features in the base class or if they need to be implemented in the derived class.

Now we’ve done some nifty
refactoring and laid the groundwork to add a new protocol so we can support some other types of host devices.

Share This