7.13 Inheritance
Starting from one class, we can derive other classes that inherit and modify the original’s behavior and functionality. Inheritance allows us to specialize instances while reusing code from the class hierarchy.
In section 1.10, we focused on Bay leaves, but here we
return to guitars and their various types. Let’s begin with a basic definition of the
Guitar
class. The full and documented code that we develop here
is in Appendix D.
class Guitar:
def __init__(self, brand, model, year_built, strings=6):
self._brand = brand
self._model = model
self._year_built = year_built
self._strings = strings
def __str__(self):
return f"{self._strings}-string " \
+ f"{self._year_built} " \
+ f"{self._brand} {self._model}"
print(Guitar("Fender", "Stratocaster", "2012"))
6...