[go: up one dir, main page]

0% found this document useful (0 votes)
54 views1 page

Code Reuse (Optional) - Coursera

The document discusses code reuse in Python through inheritance and composition. It defines an Animal class and asks the reader to create a Turtle class that inherits from Animal and sets the category to "reptile". It then asks the reader to create a Snake class that also inherits from Animal and sets the category to "reptile", demonstrating composition by creating two different animal types in the same category.

Uploaded by

Joy kiruba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views1 page

Code Reuse (Optional) - Coursera

The document discusses code reuse in Python through inheritance and composition. It defines an Animal class and asks the reader to create a Turtle class that inherits from Animal and sets the category to "reptile". It then asks the reader to create a Snake class that also inherits from Animal and sets the category to "reptile", demonstrating composition by creating two different animal types in the same category.

Uploaded by

Joy kiruba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

9/8/22, 12:13 PM Code Reuse (Optional) | Coursera

Navigate Lab Files Help

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.

In [6]: class Animal:


name = ""
category = ""

def __init__(self, name):
self.name = name

def set_category(self, category):
self.category = category

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!

In [4]: class Turtle(Animal):


category="reptile"

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.

In [7]: class Snake(Animal):


category="reptile"

https://in.coursera.org/learn/python-crash-course/ungradedLab/O497x/code-reuse-optional/lab?path=%2Fnotebooks%2FC1M5L3_Code_Reuse_… 1/1

You might also like