In the last lesson, you learned more about instance methods -- what are they and how to define them. In this lab, you are going to flesh out the driver and passenger classes by writing your own instance methods on these classes.
You will be able to:
- Define classes and instantiate instances of those classes
- Define and call instance methods
In this section, define two classes, Driver
, Passenger
.
In the Driver
class, define the instance method greeting
that returns the string "Hey, how are you?"
. Then define the method ask_for_destination
, which returns the string "Where would you like to go today?"
.
In the Passenger
class, define the instance method reply_greeting
that returns the string "I am doing well! Thanks for picking me up today!"
. Then define the method in_a_hurry
, which returns the string "Punch it! They're on our tail!"
.
Define these classes and instance methods in the cells below
Remember: as we learned in the previous lesson, we need to define our instance methods with at least one argument (
self
) in order to call them on an instance object. Don't worry, we will learn more about this argument in a later lesson.
# Define Driver class here
# Define Passenger class here
Great! You've defined classes and instance methods. Now, in this section you're going to actually use them!
Start by instantiating two drivers and two passengers. Assign the drivers to the variables daniel
and meryl
and assign the passengers to niky
and terrance
.
daniel = None # driver
meryl = None # driver
niky = None # passenger
terrance = None # passenger
Alright, you have our passengers and drivers! Now you need to put those instance methods to use. Try them out and assign the return values to the variables below. Have daniel
greet his passenger, who is going to be niky
. Assign the greeting to the variable, polite_greeting
. Have niky
respond by calling in_a_hurry
, and assign the return value to the variable, no_time_to_talk
.
polite_greeting = None
print(polite_greeting)
no_time_to_talk = None
print(no_time_to_talk)
Now, let's have some extra practice creating classes, instantiating objects, and using instance methods.
In the cells below, you'll create three different classes that represent animals in a zoo--lions, tigers, and elephants. Each animal should have a method, speak
, which returns a string containing the sound they make (feel free have some fun with this--we don't know how to spell the sound an elephant makes any better than you do!).
# create Lion class
# create Tiger class
# create Elephant class
Now, in the cell below, create an instance of each animal.
simba = None
tony = None
dumbo = None
Now, append each of them into the list zoo
in the cell below.
zoo = None
Now, loop through the zoo
list and call print out the .speak()
method for every animal in the zoo.
In this lab, you practiced defining classes and instance methods. You then instantiated instances of our classes and used them to practice calling our instance methods.