Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Basic Object-Oriented Python Creating a Memory Game Card Class

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

self.card

In the video, Megan refers to a card attribute in the __init__ method of the Card class but passes in word as an argument. But she uses self.card = word when setting it.

Is this an error?

I tried changing the code to set the attribute to self.word = word as I think it should be and the code works the same way. Was this a mistake and it should have been self.word?

(Sorry, I didn't mean to make this post sound so blunt and critical) 😊

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Jonathan Grieve, good question!

TL;DR: yes, it could, and probably would have been, a better choice to use Card.word or Card.text as the attribute name.

Since the name of an initialization parameter is not exposed, it could be anything, and the proper choice makes a difference in the readability of the code. Obviously,

def __init__(self, rockpile, location):
    self.card = rockpile
    #...

would also work, but makes it a bit confusing.

The attribute name card is exposed and has a direct effect on the readability of the code. Card.card can be argued is not as descriptive as it could be. You are correct that Card.word or even Card.text might be more appropriate. In some cases, if an attribute has already been established and exists in legacy code, a painful less-than-optimum choice lives on forever.

As to whether this is a "mistake", only those present at the code review could say. 🤷‍♂️

Post back if you wish to discuss this more. Happy coding!!