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
Learn how to add functions to a class in Python.
- Methods - functions inside of classes. They take self as an argument.
- Calling a method -
instance_name.my_method()
- Passing in values -
instance_name.my_method(βsome valueβ)
- Calling a method inside of another - use
self.my_method()
instead ofinstance_name.my_method()
(sinceself
essentially equalsinstance_name
) - Using an attribute -
self.my_attribute
when inside your class
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
In object-oriented Python, a function
inside of a class is called a method.
0:00
In this video, we're going to dive
into how to add methods to a class,
0:07
pass in arguments and call a method.
0:12
Let's start by adding
a method to this car class.
0:15
It will be called stop
0:19
The format is the same as
a regular function, but
0:31
it needs to take self as an argument.
0:34
All methods inside of a class will need to
take self, otherwise it'll throw an error.
0:37
Inside of our method,
let's just print out "The car has stopped".
0:44
Let's jump down here and call our method.
0:54
In order to call it,
we will need our instance and
0:59
dot notation plus
the name of the function.
1:02
So, car_one.stop().
1:04
Run the file.
1:12
And voila, it printed out our message.
1:15
Now, what if we needed to pass
in information to our method?
1:21
How do we do that?
1:25
Well, it works just like
a regular function.
1:27
Let's create a new method called go.
1:30
It'll take self and a speed argument.
1:41
And then print out an f string,
"The car is going" and pass in the speed.
1:46
Cool, now let's call this method a few
times and pass in a few different speeds.
2:01
Go ahead and try it on your own,
pass in whatever speeds you like.
2:07
Okay, so we've tackled creating a method,
2:30
calling a method and
passing an argument so far.
2:33
It's time for
you to give it a go on your own.
2:38
Create a class, add a method that
takes at least one argument.
2:41
Give it some logic or
a simple print statement and then call it.
2:45
If you get stuck, rewind me and
rewatch this video.
2:50
It's okay if it takes you a few times for
things to click.
2:54
I'll see you back here in a bit.
2:57
Back?
3:03
Let's keep going.
3:04
A powerful thing about classes is
we can use our attributes inside of
3:06
any method we have.
3:10
Let's add a new instance attribute
called is moving and set it to false.
3:12
Instance attributes don't
always have to be passed in.
3:25
Now we can adjust our stop and
3:30
go methods to check this attribute
first before printing a message.
3:32
That way we don't tell
a stopped car to stop more cuz
3:38
that wouldn't really make any sense.
3:42
In our stop method,
let's check if the car is moving.
3:46
If it is,
let's print out that the car has stopped
3:57
And then set our is
moving attribute to false
4:03
Otherwise, let's let them know
the car has already stopped.
4:12
Inside of our go method,
let's do something similar.
4:33
If is moving is false, which means
we need to check for, not is moving.
4:38
Then let's let the user know
the car has started moving and
4:48
set the attribute to true.
4:52
We can print out the car's speed
outside of the statement, so
5:09
the speed can be changed
if the user wants.
5:13
Nice.
5:16
Let's call both methods a few
times to see our work in action.
5:18
Run the file.
5:36
Take a minute to follow the logic
of what's happening in the console
5:39
with the code you just wrote.
5:45
For example, when is instance is created,
is moving is set to false.
5:47
So when stop is called, we get the
5:56
"The car is all ready stopped" message.
6:02
When go is called,
is_moving is set to True and
6:06
we get "The car starts moving" and
our speed message.
6:12
Keep following the rest
of the logic on your own.
6:18
This helps you make sure you know
what is happening with your code.
6:22
Go ahead and pause me.
6:25
Nice work.
6:29
One last challenge to tackle.
6:31
If the car is moving,
then it's using up gas.
6:33
Let's create a new instance
attribute called gas.
6:38
Now let's create a method called use_gas.
6:46
Inside of the method, reduce the gas
value by 50, to make it easier to test.
7:04
Then return False if the self.gas
value is less than or equal to zero.
7:18
And True, if there's still gas.
7:31
Now we can use this method
inside our go method.
7:38
If the car is moving,
7:43
then we need to call our use_gas method
to reduce the amount of gas we have.
7:45
If we are out of gas then
the car cannot move at all.
7:51
At the start of this method,
7:56
let's check if we have gas by calling
the method using self in an if statement.
7:58
This both calls the method and
8:08
lets us know if we still
have gas at the same time.
8:11
If we have gas,
then we can run the logic we already had.
8:16
Otherwise, we need to let the user
know they've run out of gas and
8:24
call the stop function to stop the car.
8:29
Go ahead and run the file to see
what's changed in the console.
8:45
Take a minute again to think through
what's happening in the console and
8:54
follow the logic in your code.
8:58
When you feel comfortable that
you understand what's happening,
9:01
then unpause me.
9:04
It's recap o'clock.
9:08
In this video,
9:11
you've learned how to add functions to
your class, which are called methods.
9:12
They follow the same syntax
as regular functions, but
9:17
are placed inside of a class and
take self as an argument.
9:21
You can pass in values to a method,
call other methods
9:27
inside of one and
access attributes of your class.
9:33
When calling a method, you use dot
notation on an instance of the class.
9:44
Don't forget to pass in values
if the method calls for them.
9:50
Try out what you've learned so
far on your own.
9:55
Create your own class, add attributes,
methods and play around.
9:58
This is how you learn and
solidify your new knowledge.
10:03
Also, try helping someone else.
10:08
Nothing helps us realize
how much we know or
10:11
don't know, by helping someone else learn.
10:14
Lastly, never feel bad if
you need to rewatch a video.
10:18
Some concepts may be harder than others
and there's no judgment on how many
10:22
times you needed to watch a video
before the concept finally clicked.
10:27
Take as long as you need and
reach out if you hit a roadblock.
10:31
We're here to help.
10:35
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