-
Book Overview & Buying
-
Table Of Contents
Python Illustrated
By :
In short, inheritance is all about creating new classes based on existing ones. This helps keep your code clean, reusable, and compact. Reusability is popular in the programming world. We even have an abbreviation for it: DRY (short for don’t repeat yourself). If you find yourself copying and pasting pieces of code, there’s usually a better way. When working with classes, this is where inheritance shines. It lets one class pass on attributes and methods to the next.
For example, we saw how Dog, Cat, and Rabbit were all types of Pet and that they inherited everything from the Pet class. Quick reminder, this is what the class syntax looks like:
class Cat(Pet):
pass

Here, the Cat class inherits everything from the Pet class simply by placing Pet inside the parentheses. You probably wouldn’t create child classes unless you plan to add some custom behavior.
To add methods to the child classes, you just define them as you would otherwise...