Creating the Dog Class (Solution)
00:00
Here I am in the new file that I saved on my computer, and I called it Doggo.py
. I’m going to start by recapping the tasks that I basically have for the Dog
class. So I should build a Dog
class.
00:17
I should create a class attribute called .species
, and then this was "Canis familiaris"
.
00:29
Then another task was that each dog should have a .name
and .age
. Also, it should print nicely
00:40 and should be able to speak
00:46
a sound
, right? So this should be some sort of argument that I can pass to the instance method.
00:53
I think these are the tasks. Let’s get started. So I’ll start by building the Dog
class, using the class
keyword and then the name of the class, which I will use in cap case. So starting off with a capital letter.
01:05
And if it would be more words like DogPark
, right, then you start a second word also with a capital letter. That’s called cap case. But this class is just called Dog
.
01:17
And I’ll add a pass
keyword down here to say that I want to add some more information into the class. So that’s my Dog
class without anything, but I do have a Dog
class. I’m going to get rid of this first comment.
01:32
Next step, create a class attribute .species "Canis Familiaris"
. We’ll just copy this here and replace the pass
keyword with species = "Canis Familiaris"
.
01:45 If I put a variable right underneath the class, then it’s part of the class. So then it becomes a class attribute and not specific to an instance of the class. So defining a variable like this makes it a class variable.
02:00
I’m done with creating the class attribute for .species
. My next step is each dog should have a .name
and an .age
.
02:09
Make sure that you keep your code indented to make it part of the Dog
. And now I need to create the initializer method, .__init__()
, which is going to run every time that I create a new instance of Dog
, I need to define this method, def __init__()
.
02:29
And so there’s double underscores, right? Double underscores at the beginning, then init
, and then another double underscores.
02:37
And then I have to put something in here. And the first thing that you always need to pass in this instance method is self
. So I’ll start off with self
. What else do we need?
02:47
We’re going to need a name
and then age
. So creating a method signature that takes three parameters, self
, name
, and age
, means that when I actually create an instance of Dog
, we’re going to have to pass two attributes, .name
and .age
.
03:04
And .self
is a reference to the instance itself. And that gets passed automatically. And then I’m just going to reassign this. I’m going to say self.name = name
.
03:16
This line of code assigns whatever is passed in as an attribute for .name
to the instance attribute .name
. And then I’ll do the same for saying self.age = age
.
03:31
And that should be it. So by defining the .__init__()
in this way, what I say is that every Dog
instance that gets created has two arguments that you need to pass when you create an instance. Otherwise, it’s going to fail.
03:43
And these are .name
and .age
, and we’ll assign them to the instance. Cool, done with this little task. Next one is that it should print nicely.
03:54
I’m going to use another dunder method, def __str__()
, __str__(self)
, always the first one. And here I don’t need to pass anything else because I will just access the instance attributes and then create a string and return that specific string. So that’s going to be printed when I call print()
and pass it an instance of the Dog
class. Here I will return—let’s use is an f-string, easier to format—self.name
, So the name of that specific dog.
04:30
And I will tell the user that the dog is self.age
years old. So in that case, I get the name and the age of the dog, which are the two pieces of information that we have about the dog.
04:47 And I think that’s going to print nicely.
04:50
And then we have a final task, which says that it should be able to speak a sound
. I can pass that sound as an argument. So let me define another instance method.
04:59
This is not going to be a special method like the other two. This is actually going to be a public method of that class, and I’m calling it .speak()
, and it takes self
and it also takes a sound
. So again, self
is the reference to the instance itself.
05:15
You’re not going to have to pass that because Python does that for you. But then it takes one argument, and then I want to again return an f-string that gives the information of who it is, so the name of the dog, and then it says whatever sound you passed in. Note that here, you don’t use self.sound
, right, because this is not an instance attribute, but this is just an argument that you are passing to this method when you’re calling it. And I think that should be it.
Become a Member to join the conversation.