Inheritance

Inheritance is the idea that a class which is created off the back of another class can use features from said other class. This subclass can also change them, or build upon them as necessary.

Let's use inheritance to alter the behavior of some dogs. Add these new classes underneath your Dog class:

class Greyhound(Dog):
def __init__(self, name):
super().__init__(name)

def speak(self):
print("Zoom! My name is", self.name)

def race(self, opponent):
print(self.name, "is running faster than", opponent.name)

class JackRussell(Dog):
def __init__(self, name, color):
super().__init__(name)
self.color = color

def get_color(self):
print(self.name, "is", self.color)

In order to inherit from an existing class, we place its name in brackets after the name of our new class. This ensures that Greyhound is a subclass of Dog.

When initializing our Greyhound instance, we can reuse the initializing code from our Dog class by accessing it via super(). Once again, the self argument is passed automatically, so we only need to pass the name over to the __init__ function of Dog. The name attribute of our Greyhound will be set using the code from Dog, so we do not need to do anything more in our init of Greyhound.

 

The Greyhound class also demonstrates the ability to overwrite methods in the original class by simply declaring another one with the same name. We redefine speak here so that our more specific speak method of our Greyhound  will be called instead of the Dog one from any Greyhound instance.

We can add new methods to the Greyhound class as normal. Here, we have added a race method.

The race method demonstrates another important concept which occurs with using classes: polymorphism. The argument passed to this method can be anything, since Python is dynamically typed. This means that we can use any class instance which has an attribute called name as the opponent in our race method. Since our Dog class defines a name attribute, we can use any kind of Dog to race against our Greyhound.

The JackRussell class shows that additional, more specific attributes can be added onto classes which derive from another within the __init__ method. These attributes behave as normal.

Let's try out these new classes. Remove the code regarding dog_one and dog_two and add this in its place:

greyhound = Greyhound("Tessa")
jack_russell = JackRussell("Jack", "brown")
dog = Dog("Boris")

greyhound.speak()
jack_russell.speak()
dog.speak()

greyhound.race(jack_russell)
greyhound.race(dog)

jack_russell.get_color()

The preceding code first demonstrates that each Dog still has the ability to speak, and the Greyhound will speak differently to the others. Note that even though the JackRussell class does not have a speak method defined, it has acquired it from the base class – Dog.

We then show that either type of Dog will work for racing against the Greyhound, since both have a name attribute.

Finally, we demonstrate that the JackRussell has access to its unique color attribute as normal.

Try adding dog.get_color() after the JackRussell class call. You should get an AttributeError, since inheritance is only one way. JackRussell can call the Dog speak method, since the JackRussell inherits from it, but the Dog class does not receive anything back from the JackRussell class. This is important to remember when using polymorphism to accept multiple classes as method arguments. Always ensure that the method relies on attributes of the base class, not a specific attribute from a subclass.

Now that we have an understanding of how to write and use classes, we can begin writing our blackjack game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset