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 trialKin Yu Wong
1,653 PointsCan __eq__ do a 3 way comparison among 3 instances?
Can eq do a 3 way comparison among 3 instances?
1 Answer
Steven Parker
231,533 PointsThe __eq__ method allows you to redefine what the equality operator (==
) does, but the syntax of the operator itself limits it to working only with two operands. You could, however, combine multiple instances of the normal equality operator using the logical "and". For example:
if FirstThing == NextThing and NextThing == LastThing:
# all 3 are equal
You can also define a new function or method which tests for equality among three (or more) parameters:
def equals(*args):
for a in args:
if args[0] != a:
return False
return True