Code Reuse (Optional) - Coursera
Code Reuse (Optional) - Coursera
Code Reuse
Let’s put what we learned about code reuse all together.
First, let’s look back at inheritance. Run the following cell that defines a generic Animal
class.
What we have is not enough to do much -- yet. That’s where you come in.
In the next cell, define a Turtle class that inherits from the Animal class. Then go
ahead and set its category. For instance, a turtle is generally considered a reptile. Although
modern cladistics call this categorization into question, for purposes of this exercise we will
say turtles are reptiles!
Run the following cell to check whether you correctly defined your Turtle class and set its
category to reptile.
In [5]: print(Turtle.category)
reptile
Was the output of the above cell reptile? If not, go back and edit your Turtle class making
sure that it inherits from the Animal class and its category is properly set to reptile. Be
sure to re-run that cell once you've finished your edits. Did you get it? If so, great!
Next, let’s practice composition a little bit. This one will require a second type of Animal
that is in the same category as the first. For example, since you already created a Turtle
class, go ahead and create a Snake class. Don’t forget that it also inherits from the
Animal class and that its category should be set to reptile.
https://in.coursera.org/learn/python-crash-course/ungradedLab/O497x/code-reuse-optional/lab?path=%2Fnotebooks%2FC1M5L3_Code_Reuse_… 1/1