"Java Objects (Retired)" was retired on January 31, 2017. 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
Create the logic for the card class for our memory game.
Accomplished in this Video
- Created a Card class
- Gave it two instance attributes that are passed in - the word for the card and its location
- Added another instance attribute that is not passed in - matched
- Created 2 dunder methods
-
__eq__
= check if two cards are equal -
__str__
= print out the word for a card
-
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
Let's get to building the card class.
0:01
In the init method,
it'll take two arguments.
0:11
One will be the word each card will
hold and the second, its location.
0:18
Inside of the method,
we'll need to set the card attribute and
0:25
the location attribute.
0:28
Then create a matched attribute
that is set to false, so
0:38
we can track when a card has been matched.
0:41
Great, now let's tackle
checking if two cards
0:48
are equal by adding
a Dunder equals method.
0:51
It'll take self and other, and inside,
1:01
it should return true or
false if the cards match.
1:04
Lastly, the ability to print out a card,
add a Dunder string method.
1:15
Return a string with the card inside.
1:28
Now let's add a Dunder main so
we can test out our code.
1:34
This way, we don't have to
worry about our code running
1:38
when we import our card class
into our game class later.
1:43
Create three instances.
1:48
Make sure two of them would be equal
by having the same word passed in.
2:23
Now let's check them by
printing out some comparisons.
2:30
The first one should return true,
while the second one should return false.
2:46
Then let's also print out a card to make
sure the Dunder string method is working.
2:51
Let's run the file.
2:59
And great, it's all working.
3:04
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