thefence2113
thefence2113

Reputation: 145

Modifying class in Python using __eq__

I need to Add an eq method that returns True if coordinates refer to same point in the plane (i.e., have the same x and y coordinate) and am confused on how to do that.

I have tried some code using eq and I am still getting errors and I am not quite sure why.

class Coordinate(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y
    def getX(self):
        # Getter method for a Coordinate object's x coordinate.
        # Getter methods are better practice than just
        # accessing an attribute directly
        return self.x
    def getY(self):
        # Getter method for a Coordinate object's y coordinate
        return self.y
    def __str__(self):
        return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'
    def __eq__(Coordinate, otherPoint):
        if self.GetX() == otherPoint.getX()&& self.GetY() == otherPoint.getY()
            return True

x=5
y=5

The expected output would return true if both coordinates were the same number and false if x and y were not the same numbers.

Upvotes: 1

Views: 905

Answers (3)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

Some issues/fixes in your code

  • You actually don't need GetX and GetY, you can refer the attributes x and y directly.
  • && is not valid syntax in Python, instead you use and

  • The first argument of __eq__ will be self

  • You can use f-strings to format your string if you are using python3.6+, else you can use format strings
  • You can optionally raise a TypeError to ensure other is of type Coordinate

Hence the updated code will look like

class Coordinate(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        #Use a f-string to format your string representation
        return f'<{self.x},{self.x}>'

        #If python3.5 and below, you can use format string as below
        #return '<{},{}>'.format(self.x, self.y)

    def __eq__(self, other):
        #If other is not an instance of Coordinate, raise TypeError
        if not isinstance(other, Coordinate):
            raise TypeError('An instance of class Coordinate is expected')
        #Compare both coordinates and return True or False
        return self.x == other.x and self.y == other.y

Then you can use the class like as follows

c1 = Coordinate(5,5)
c2 = Coordinate(6,6)

print(c1)
#<5,5>
print(c2)
#<6,6>
print(c1 == c1)
#True
print(c1 == c2)
#False
print(c1 == 1)
#TypeError: An instance of class Coordinate is expected

Upvotes: 3

Finomnis
Finomnis

Reputation: 22748

So many small mistakes ...

  • GetX is not the same as getX
  • an if statements needs a : at the end
  • the indentation of __init__ clause is wrong
  • && does not exist in python, it's called and
  • the __eq__ function does not always return, it needs an else clause, or just return the boolean expression directly
  • the first argument of __eq__ needs to be self
class Coordinate(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def getX(self):
        # Getter method for a Coordinate object's x coordinate.
        # Getter methods are better practice than just
        # accessing an attribute directly
        return self.x
    def getY(self):
        # Getter method for a Coordinate object's y coordinate
        return self.y
    def __str__(self):
        return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'
    def __eq__(self, otherPoint):
        return self.getX() == otherPoint.getX() and self.getY() == otherPoint.getY()

Upvotes: 2

Işık Kaplan
Işık Kaplan

Reputation: 3002

>>> class Coordinate:
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...     def __eq__(self, other):
...         if not isinstance(other, Coordinate):
...             raise TypeError('You can compare a Coordinate with only another Coordinate')
...         return self.x == other.x and self.y == other.y
...
>>> Coordinate(1,2) == Coordinate(1,2)
True
>>> Coordinate(1,2) == Coordinate(1,3)
False
>>> Coordinate(1,2) == 'Hello'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in __eq__
TypeError: You can compare a Coordinate with only another Coordinate
>>>

Upvotes: 3

Related Questions