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

Akshaan Mazumdar
Akshaan Mazumdar
3,787 Points

I compiled the same code in IDLE and it does not show the same result. Both comparisons returned False.

class Cards:
    def __init__(self,word,location):
        self.word = word
        self.location = location
        self.matched = False


        def __eq__(self,other):

            return self.word == other.word

        def __str__(self):
            return self.word


if __name__ == '__main__':

        card1= Cards('egg', 'A1')
        card2 = Cards('egg','B1')
        card3 = Cards('hut', 'D4')



        print(card1 == card2)
        print(card1 == card3) 
        print(card1)




### Also my print returns object details:
###
False
False
<__main__.Cards object at 0x7fc8733dc250>
####

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Akshaan Mazumdar, it looks like the methods __eq__ and __str__ are indented too far making then inside the __init__ method.

In effect, these methods are defined as a method within a method and are not accessible for use by comparisons or printing.

Post back if you need more help. Good luck!!!