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 Emulating Built-ins __eq__

Leah Kelly
Leah Kelly
4,141 Points

Isn't there a simpler way to return a compared value than: return self.model == other.make and other.model == self.make?

Can you just:

def __eq__(self):
  return self.make == self.model

I'm quite confused.

4 Answers

Steven Parker
Steven Parker
231,236 Points

Be careful not to mix up your terms, you want to compare the make with the make, and the model with the model. The make will likely not ever match the model in the same object or in 2 different ones.

You need to compare both to be sure the items are equal, and that's why there are two comparisons joined with the and logical operation. That requires them both to be true for the result to be true.

And you need to compare the "self" with the "other". The "self" is the object you are running the test on, and the "other" is the object it is being compared to. Comparing two "self" attributes won't reveal anything useful.

Leah Kelly
Leah Kelly
4,141 Points

What about:

def __eq__(self, other):
  return other.make == other.model

Would that return the same value? thanks!

Steven Parker
Steven Parker
231,236 Points

That's still mixing up "make" and "model" .. just using the other ones instead of the internal ones. But yea, they are also not likely to match. Note that in the video, only the similar attributes get compared.