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 would not want to have to copy and paste functions between them; it would be 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 accelerateTo(self, speed):

self.speed = speed def setColor(self, color): self.color = color mycar = car() print mycar.color

That creates a car class with a default color and also provides a setcoior() function so people can change their own colors. Now, what do you drive to work? Is it a car? Sure it is, but chances are it is a Ford, or a Dodge, or a Jeep, or some other makeyou don't get cars without a make. On the other hand, you do not want to have to define a class Ford, give it the methods acceierateTo(), setcoior(), and however many other methods a basic car has and then do exactly the same thing for Ferrari, Nissan, and so on.

The solution is to use inheritance: Define a car class that contains all the shared functions and variables of all the makes and then inherit from that. In Python, you do this by putting the name of the class from which to inherit inside parentheses in the class declaration, like this:

class car(object): color = "black" speed = 0

def accelerateTo(self, speed):

self.speed = speed def setColor(self, color): self.color = color class ford(car): pass class nissan(car): pass mycar = ford() print mycar.color

The pass directive is an empty statementit means our class contains nothing new. However, because the ford and nissan classes inherit from the car class, they get color, speed, accelerateTo() , and setcolor() provided by their parent class. Note that we do not need object after the class names for ford and nissan because they are inherited from an existing class: car.

By default, Python gives you all the methods the parent class has, but you can override that by providing new implementations for the classes that need them. For example:

class modelt(car):

def setColor(self, color):

print "Sorry, Model Ts only come in black!"

myford = ford() ford.setColor("green") mycar = modelt() mycar.setColor("green")

The first car is created as a Ford, so setColor() will work fine because it uses the method from the car class. However, the second car is created as a Model T, which has its own setColor() method, so the call will fail.

This provides an interesting scenario: What do you do if you have overridden a method and yet want to call the parent's method also? If, for example, changing the color of a Model T was allowed but just cost extra, we would want to print a message saying, "You owe $50 more," but then change the color. To do this, we need to use the class object from which our current class is inheritedcar in this example. Here's an example:

class modelt(car):

def setColor(self, color):

print "You owe $50 more" car.setColor(self, color)

mycar = modelt() mycar.setColor("green") print mycar.color

That prints the message and then changes the color of the car.

Was this article helpful?

0 0

Post a comment