Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python equals override

You need to be careful with inheritance:

>>> class Foo:
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        else:
            return False

>>> class Bar(Foo):pass

>>> b = Bar()
>>> f = Foo()
>>> f == b
True
>>> b == f
False
Check types more strictly, like this:

def __eq__(self, other):
    if type(other) is type(self):
        return self.__dict__ == other.__dict__
    return False
Besides that, your approach will work fine, that's what special methods are there for.
Comment

PREVIOUS NEXT
Code Example
Python :: pandas isin 
Python :: how to make my discord bot shut down with a command 
Python :: multiple lines input python 
Python :: how to read files in python with 
Python :: how append a directory based on current directory python 
Python :: python tkinter grid 
Python :: tweepy auth 
Python :: custom django user model 
Python :: python list add first 
Python :: pandas replace last cell 
Python :: python tkinter label 
Python :: cv2 get framerete video 
Python :: python split input to list 
Python :: # find out indexes of element in the list 
Python :: Python Tkinter Button Widget Syntax 
Python :: django update request.post 
Python :: hashlib sha 256 
Python :: c++ call python function 
Python :: python division 
Python :: add to a list python 
Python :: open word document python 
Python :: how to check if python is installed 
Python :: tensorflow to numpy 
Python :: python turtle 
Python :: try with multiple except python 
Python :: sns how to change color if negative or positive 
Python :: save model history keras 
Python :: coloring text in python 
Python :: python private method 
Python :: gradient boosting regressor 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =