Maximum recursion error in `__eq__` implementation

2024/10/6 14:26:29
class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate object's x coordinate.# Getter methods are better practice than just accessing an attribute directlyreturn self.xdef getY(self):# Getter method for a Coordinate object's y coordinatereturn self.ydef __str__(self):return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'def __eq__(self, other):if self.getX == other.getX and self.getY == other.getY:return Trueelse:return False        c = Coordinate(2, 3)
d = Coordinate(2, 3)c == d
Answer

Your __eq__ method should probably be like this:

It avoids comparing objects of different type and alerts of the error immediately, and avoids the use of getters intra class.

def __eq__(self, other):if self.__class__.__name__ == other.__class__.__name__:return self.x == other.x and self.y == other.yelse:raise NotImplementedError

or in python > 3.3:

def __eq__(self, other):if self.__class__.__qualname__ == other.__class__.__qualname__:return self.x == other.x and self.y == other.yelse:raise NotImplementedError
https://en.xdnf.cn/q/118937.html

Related Q&A

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…

cnn news webscraper return empty [] without information

so i wrote this code for now: from urllib import request from bs4 import BeautifulSoup import requests import csv import reserch_term = input(What News are you looking for today? )url = fhttps://editi…

Why the code shows all the addition process?

Code: sum=0 for i in range(10,91):sum=sum+iprint(sum)When I wrote this code, the answer was Output: 10 21 33 46 60 75 91 108 126 145 165 186 208 231 255 280 306 333 361 390 420 451 483 516 550 585 621 …

Creating a list of keywords by scrolling through a dataframe (python)

I have a dataframe that looks like this: dataFrame = pd.DataFrame({Name: ((" Verbundmrtel , Compound Mortar , Malta per stucchi e per incollaggio "),(" StoLevell In Absolute , StoLeve…

How to click this button with python selenium

Im looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.Im having trouble identifying the button to…

How to find the average of numbers being input, with 0 breaking the loop?

I just need to figure out how to find the average of all these input numbers by the user while using 0 as a exit of the loop. I need to figure out how to eliminate using 0 as part of the average. examp…

NoSuchElementException when loading code using Selenium on Heroku

Error: ERROR:asyncio:Task exception was never retrieved2022-03-14T14:08:52.425684+00:00 app[worker.1]: future: <Task finished name=Task-30 coro=<Dispatcher._process_polling_updates() done, define…

Python alphanumeric

Problem:I have to go through text file that has lines of strings and determine about each line if it is alphanumeric or not. If the line is alphanumeric print for example "5345m34534l is alphanume…

Python 3 - exec() Vs eval() - Expression evaluation [duplicate]

This question already has answers here:Whats the difference between eval, exec, and compile?(3 answers)Closed 7 years ago.After reading query.below python code is still not clear,>>> exec(pri…

What is the syntax for printing multiple data types in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…