Pet Lab
This lab introduces you to object oriented programming, a paradigm which is the focus of the third unit. Programming paradigms are ways of thinking about problems, which lead to ways of solving them by using code in certain ways. In imperative programming (the drawing unit), we solve problems in a straightforward way: by giving instructions on what needs to be done. In functional programming (the data science unit), we instead think about problems in terms of how data will be transformed. We break large problems down into smaller problems by writing functions which perform part of the needed transformation.
Object-oriented programming (OOP), in contrast, involves creating a model of the problem situation, with objects representing components of the problem. The most important new construct we will introduce in OOP is classes. A class describes a type of object; an instance of a class is a specific object of that type. We will play with classes first and then look more closely at the code.
Creating your own pet
As always, make sure you are in this lab’s directory, and have entered a Poetry shell.
In pet.py
is a Python class that defines a Pet
.
๐ป
First, let’s run pet.py
in the interactive Python shell. Now the class Pet
will
be defined.
python -i pet.py
๐ป
Create an instance of Pet
and store it in the variable my_pet
.
>>> my_pet = Pet()
๐ป Now that you've created a pet, let's give it a name. I’ve named mine Ajax. You can name yours whatever you’d like.
>>> my_pet.set_name("Ajax")
>>> my_pet.introduce()
Hi, Im Ajax
>>> my_pet.play()
>>> my_pet.nap()
โ CHECKPOINT:
In your notebook, answer the questions below before moving on.
- What happens if you try to get your pet to take two naps in a row?
- What happens if you play twice in a row without napping in between?
- Can you change the name after it’s already set?
๐ป Exit the Python shell interface by typing
^D
.
Your pet and its name will not be saved.
How do classes work?
Now that you’ve used the Pet
object, let’s see how the code works.
๐ป
Open pet.py
in atom. Here are the first fifteen lines:
|
|
- Line 1 defines a new class. Like a function declaration, a class declaration ends with a colon and is followed by a code block.
- Lines 2, 8, and 12 look like function declarations (they start with
def
), but they’re inside of the class. These are called methods, and can be called with every instance of a class. (When you ranmy_pet.set_name("Ajax")
above, it called theset_name
method on line 8. When you calledmy_pet.introduce()
, it called the function on line 12. - Line 2 is a special method,
__init__
, which is called when an instance is created. When you ranmy_pet = Pet()
above,__init__
was called.__init__
is short for initialize, and should be used for any needed setup. In thePet
class,__init__
sets the instance’s attributes to their initial values.self.name
is None,self.tired
is False, andself.bored
isTrue
. Pets start off nameless and bored, but not tired. (__init__
is optional. If it is not defined for a class, nothing special happens when new instances are created.)
Every class method has a special first
argument, self
, which is provided automatically when the method is called.
self
is the instance calling the method. You can use self
to access other
functions and variables of a class instance. For example, when you ran my_pet.set_name("Ajax")
above, line 10 set that instance’s name attribute to "Ajax"
. When you ran my_pet.introduce()
above, line 14 accessed that instance’s name attribute.
One way to think of a class as a bundle of related functions (called methods) and variables (called attributes).
What species is your pet?
Now let’s make our Pet
more complex. People can have all different species of pets, so let’s
add a species
attribute to our Pet
.
Adding a new attribute
If you look in the Pet class, you can see on lines 5-7 that we define three attributes. Attributes are variables that belong to a specific class.
|
|
The
Pet
currently 3 attributes,name
,tired
, andbored
.
๐ป
Add a species
attribute to the Pet
class.
Just like the name
attribute, its initial value should be None
.
Adding a new method
Now that we’ve added the species
attribute, we need to add a method to change the attribute.
Just like the name
attribute, we need to be able to set the species
of our pet.
๐ป Add a new method called
set_species()
. This method should be almost identical
to set_name
(shown below). It should accept a species
argument (in addition to self
), and should
set self.species
to the value passed in.
|
|
Testing your changes
Let’s see if the species
attribute and set_species()
method is working by jumping back into the Python shell.
python -i pet.py
๐ป Test your changes by typing these in the terminal one at a time. You can set your species to whatever kind of pet you want!
>>> my_pet = Pet()
>>> my_pet.set_species("fox")
>>> my_pet.species
fox
Remember, when you’re finished using the shell, you can exit by typing ^D
.
Your pet and its species will not be saved.
Using the species attribute
Right now, the introduce()
method just has the pet say their name. Let’s make it more detailed by including their species
in the introduction.
|
|
๐ป
Edit the introduce()
method so that your pet will also tell you its species.
๐ป
Test the updated introduce()
method using the shell. If you need a reminder of how to do this, check the earlier examples at the beginning of the lab.
๐พ ๐ฌ
Line 16 uses an f-string, or a string with a'f'
prefix. F-strings allow you to drop variables right into the string by surrounding them with curly braces.
What’s wrong with my pet?
Wouldn’t it be nice if your pet could tell you whether it wanted to play or take a nap? Right now, the only way to know for sure is to check its attributes.
If tired
is True
, then the pet needs a nap()
. If bored
is True
, the pet wants to play()
.
Let’s add a new method that allows the Pet
to communicate.
๐ป
Add a new method called status()
. It should print out what the Pet
currently needs.
Be sure consider the current state of the tired
and bored
attribute.
python -i pet.py >>> my_pet = Pet() >>> my_pet.set_name('Peanut') >>> my_pet.set_species('Dog') >>> my_pet.status() "I'm bored! Let's go for a walk!"
Extension
[Upgrade the Interface]
At this point, you have a working pet with multiple features! Now, let’s develop the interface to make the Pet
more like a video game.
๐ป
Run interface.py
. The Pet
now has a nice Terminal interface where you can interact with it like a video game.
However, currently all you can do it:
- Create a
Pet
- Set its
name
play()
with it
๐ป
Edit interface.py
to include all of the features the Pet
has.
๐ป
Add any additional features to the Pet
and the interface!