Class Inheritance
Python allows you to reuse your code by inheriting one class from one or more others. For example, lions, tigers, and bears are all mammals and so share a number of similar properties. In that scenario, you do not want to have to copy and paste functions between them; it is smarter (and easier) to have a mammal class that defines all the shared functionality and then inherit each animal from that.
Consider the following code:
class Car(object): color = "black" speed = 0 def accelerate_to(self, speed): self.speed = speed def set_color(self, color): self.color = colormycar = Car()print mycar.color
That creates a Car class with a default color and provides a set_color() function so that people can change ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access