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

This isn't printing if it is equal or not equal. It just gives me an error message!

I tried:

class Shoes():
  def __init__(self, brand, color, size=2):
    self.brand = brand
    self.color = color
    self.size = size

  def __eq__(self, other):
    return self.other == other.color and self.brand == other.brand

my_shoe = Shoes("Nike", "blue")
my_shoe2 = Shoes("Nike", "neon green")

if my_shoe == my_shoe2:
  print("eq")
else:
  print("not eq")

but it just gives:

Traceback (most recent call last):
File "/home/treehouse/workspace/shoes.py", line 13, in <module>
if my_shoe == my_shoe2:
File "/home/treehouse/workspace/shoes.py", line 8, in eq
return self.other == other.color and self.brand == other.brand #and self. == other.color
AttributeError: 'Shoes' object has no attribute 'other'

2 Answers

Steven Parker
Steven Parker
231,268 Points

On line 8 you have "self.other", but you probably meant to write "self.color" instead.

Leah Kelly
Leah Kelly
4,141 Points

thanks a lot! I didn't see that.