Get startedGet started for free

Classes and the DRY principle

1. Classes and the DRY principle

Great work on the exercises, your package now has a handy Document class that can be used to analyze any generic text. What if we wanted to specialize this functionality to focus on the social media data we've been analyzing?

2. Creating a SocialMedia Class

To do this let's make a SocialMedia class that performs the same functions as the Document class, but can additionally analyze social media specific things like hashtags. However, when making this SocialMedia class we want to preserve the Document class as is to be used for more general analysis. So, how can we do this? A first guess might be to copy-paste the contents of the Document class. This violates what is known as the DRY principle in Software Engineering.

3. The DRY principle

The DRY principle is a great rule that, if followed, can help you write modular, readable code. DRY stands for:

4. The DRY principle

Don't.

5. The DRY principle

Repeat.

6. The DRY principle

Yourself. There are many ways to follow this rule such as writing re-usable functions, classes, and packages. The benefits of staying DRY are not only saving time by reusing code, but also if you copy-paste code, and later find a bug, you have to remember everywhere you've pasted the buggy code and fix each instance individually. If you stay DRY you only need to fix the bug once.

7. Intro to inheritance

In the case of extending the Document class to be a SocialMedia class, we can stay DRY by using the Object Oriented Programming concept of inheritance. With inheritance, we start with a parent class and we pass on it's functionality to a child class. The child class inherits all the methods and attributes of its parent, and we're able to add additional functionality without affecting the parent class.

8. Inheritance in Python

So how do we leverage inheritance in Python? To start let's see how the child SocialMedia class fits into the package structure. It will live as a single file named after the SocialMedia class at the same level as the rest of the package's code.

9. Inheritance in Python

Now let's see how to actually write a child class that uses inheritance. You'll be writing the code for the SocialMedia class, so we'll stick to a generic example. First, we import the ParentClass for use in defining our ChildClass. To let Python know we're using inheritance, we pass the ParentClass as an argument in our class statement. Last, we call our ParentClass's __init__ method. Remember, __init__ builds an instance of a class and it also accepts self as its first argument. With this call, we're building an instance of ParentClass and storing it right back into self. This means that self now has all the methods and attributes that an instance of ParentClass would. We can now use self as normal to build in additional functionality unique to ChildClass. Here, we just add an attribute. With our definitions, we can now create an instance of our new ChildClass as we've seen before, and then access attributes from both the parent and the child. Using inheritance like this can lead to short, easy to read definitions of children classes that are jam-packed with the functionality of their parents.

10. Let's Practice

We just covered the important concepts of DRY and class inheritance. You'll now use both of these concepts by building a custom SocialMedia class that extends the Document class you've already written. Good luck!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.