"Python Basics (2015)" was retired on June 22, 2018. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Basic Object-Oriented Python!
You have completed Basic Object-Oriented Python!
Preview
Tackle equal comparisons between two objects.
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
Comparisons are an important
feature of programming.
0:00
When it comes to comparing
two objects you've created,
0:04
Python will need to know what to do.
0:08
For example, using our car class,
how would two instances compare?
0:12
Let's just stick with equals right now.
0:21
What would make two car instances equal?
0:24
Is it matching makes and models or
matching makes, models and years?
0:28
Let's just keep it to makes and models.
0:34
We'll need to add the Dunder method
that handles equal comparisons to our
0:37
car class, it's Dunder equals.
0:42
It takes self and other which is
the thing you're comparing it to.
0:56
Inside the method, let's check if the make
and models are equal and return it.
1:02
This will give us the true or
1:24
false boolean value
the comparison is looking for.
1:26
Let's test it out with an if statement.
1:30
If one instance is equal to another,
1:33
we can print out a message
saying they're equal.
1:35
Otherwise, print out a message
saying they're not equal.
1:39
So with our two instances right now,
we get not equal when the file is run.
2:04
But if we switch out the make and
model to be equal,
2:12
Run it again.
2:22
We get equal.
2:29
Try this out on your own by creating
a new class, adding an equal method, and
2:31
testing it out with some comparisons.
2:36
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