15. OBJECT-ORIENTED PROGRAMMING AND INHERITANCE¶
Defining a function and calling it from several places saves you from having to copy and paste source code. Not duplicat- ing code is a good practice, because if you need to change it (either for a bug fix or to add new features), you only need to change it in one place. Without duplicate code, the program is also shorter and easier to read.
Similar to functions, inheritance is a code reuse technique that you can apply to classes. It’s the act of putting classes into parent-child relationships in which the child class inherits a copy of the parent class’s methods, free- ing you from duplicating a method in multiple classes.
Many programmers think inheritance is overrated or even dangerous because of the added complexity that large webs of inherited classes add to a program. Blog posts with titles like “Inheritance Is Evil” are not entirely off the mark; inheritance is certainly easy to overuse. But limited use of this technique can be a huge time-saver when it comes to organizing your code.
- 15.1. How Inheritance Works
- 15.2. The isinstance() and issubclass() Functions
- 15.3. Class Methods
- 15.4. Class Attributes
- 15.5. Static Methods
- 15.6. When to Use Class and Static Object-Oriented Features
- 15.7. Object-Oriented Buzzwords
- 15.8. When Not to Use Inheritance
- 15.9. Multiple Inheritance
- 15.10. Method Resolution Order
- 15.11. Summary