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 ...
Get Ubuntu Unleashed 2015 Edition: Covering 14.10 and 15.04, Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.