Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Check your work to see if you remembered your OOP vocabulary.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Ready to see my solution?
0:00
Let's dig in.
0:01
The first step was to create
a class called Treehouse.
0:03
So I'm gonna do a couple spaces here.
0:06
And let's do class, and remember the name of
the class should be capitalized, so Treehouse.
0:09
Oops, I need two S's there.
0:17
There we go.
0:20
Okay, inside of our class, we need to add
0:21
a class attribute called product, product,
0:26
and set it equal to
the string coding courses.
0:31
And you know what?
0:36
I'm just gonna copy this, copy, paste.
0:37
Perfect, two steps done.
0:43
Three, add an initializer method
that takes a parameter called name.
0:44
I'm gonna put a space here, and
0:49
our initializer method is our def,
define, dunder init.
0:53
And you can see it kinda adds
some stuff automatically here,
0:58
thanks to my Visual Studio Code.
1:03
And inside, it's going to always
take self and we're gonna add name.
1:05
So number three complete.
1:11
Number four,
1:16
add a name instance attribute set equal
to the parameter being passed in.
1:17
So what we wanna do is set
an instance attribute,
1:23
which are attributes inside
of our initializer method.
1:26
And we wanna set our name
attribute that's being passed in.
1:29
So self.name is our attribute,
instance attribute, and
1:33
we're setting that equal to name
that's being passed in, perfect.
1:38
Number four is complete.
1:46
Number five, we're going to
create a method called learn.
1:48
Let's do that step first.
1:51
Add another space here, def,
and method is called learn.
1:54
All methods inside of classes
will need to take self.
1:59
Inside, the method should return
2:01
a string, so let's set that up.
2:07
Return, have a string.
2:12
It needs to contain the name
instance attribute.
2:13
So you would get something
like Tim is learning to code,
2:19
where Tim is the name being
passed into our class.
2:22
So I'm gonna copy our
string here actually.
2:25
We'll just replace this.
2:27
Okay, and Tim, I forgot,
2:30
it's going to give us the funky.
2:35
If you notice that, it's kind of hard
to tell, it's the same thing up here,
2:40
I'm gonna have to change those, but
2:45
it's actually a different
type of quotation mark.
2:47
That happens sometimes when you copy and
2:50
paste from like a Word doc
into your coding file.
2:53
So you can see when I change it to
what I usually use for double quotes,
2:56
it shows something different.
3:01
So just as a heads up, make sure you
have your right quotations in there.
3:03
Okay, with that fixed,
now we can get back to it.
3:08
So I'm gonna turn this into an f string
because we need to add this variable,
3:11
our instance attribute, into our string.
3:17
So that when we create a class and
we pass in a name,
3:20
that is what will be passed into this
method and printed out to the screen.
3:23
So we need our brackets.
3:29
And then to access our instance attribute,
we do self dot,
3:31
and you can see up there at the top,
it's already got it for us.
3:37
So I can hit Enter, name, and you can
see it says variable name, perfect.
3:42
Now number six,
create an instance of the class and
3:50
save it to a variable named my_treehouse.
3:54
Pass in your name as the name parameter.
3:58
All right, so let me get out of our class,
give a little space here.
4:02
Variable needs to be called my_treehouse,
set it equal to an instance of the class.
4:07
So we're gonna call the class just
like we would call a function.
4:13
And then you can see it's looking for
a name,
4:16
it's giving us a hint here
in Visual Studio Code.
4:19
Because I need to pass
in that name variable,
4:22
it's going to look for
it because it's required, right here.
4:25
So I'm gonna pass in my name,
which is Megan.
4:30
And then our final step, call your
learn method and print out the result.
4:34
So calling our method, we need to do,
I'm just gonna use our
4:42
variable because that is equal
to our instance of our class.
4:47
And then to call our method, .learn,
and you can see it pops it up.
4:52
Remember, learn is our method, and
4:58
it even tells you right here when you
highlight over it in Visual Studio Code.
5:00
And then it automatically popped in the
parentheses for us because it's a function
5:04
that we're going to call,
a method is a function inside of a class.
5:09
Now, this is going to return something,
5:14
which is why I'm asking
you to print it out.
5:15
So I'm gonna wrap this
in a print statement,
5:18
Hit Save, and now I can run my file.
5:23
So python, I'm on a Mac, so I have to
do 3 cuz I have a couple different
5:29
versions of Python on my computer.
5:33
And then the file name,
which is oop_practice.py.
5:35
Hit Enter, and you should get your string.
5:39
So I pass in Megan as my name, which is
set as my instance attribute self.name,
5:43
which is being used inside of this
method which returns this string.
5:50
And we're calling that method down
here and printing it all out,
5:57
which is getting sent to our console,
Megan is learning to code.
6:02
I hope you enjoyed the OOP
vocabulary practice session here.
6:07
Keep practicing, keep working on your OOP,
and it'll definitely start to stick.
6:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up