-
Book Overview & Buying
-
Table Of Contents
Python Illustrated
By :
Before we dive into how to do this, let’s make sure we understand why we want to do it. The main reason is to avoid duplicate code. We don’t want to have duplicate code, but write reusable code once instead.
Let’s talk about code reusability. In coding, we don’t like repeating ourselves. There’s even an abbreviation for it: DRY. That stands for Don’t Repeat Yourself.

Imagine we already have a Pet class. It has attributes such as name and age, and methods such as eat and sleep. Now, let’s say we want to create classes for Cat, Dog, and Axolotl. Should we just copy and paste all the code from Pet into each of those classes?
No!
Whenever you feel the need to copy and paste, there is typically something better that you should be doing. We’ve already seen functions; these are great for not repeating your code. We’ve seen classes, also great for not repeating...