Implement __eq__ etc. generically in Python class

If you want to apply the comparison operator to the defined class, implement the rich comparison method. Considering a class that represents a person as an example, it looks like this:

class Person(object):
    def __init__(self, firstname, lastname, email):
        self.firstname = firstname
        self.lastname = lastname
        self.email= email
    def __eq__(self, other):
        if other is None or not isinstance(other, Person): return False
        #The following is troublesome
        return self.firstname == other.firstname and
            self.lastname == other.lastname and
            self.email == other.email
    def __ne__(self, other):
        return not self.__eq__(other)

To be honest, it's annoying to write self.xxx == other.xxx and ... every time you define a class. So I will use the dict method inherited from the base class.

class Person(object):
    def __init__(self, firstname, lastname, email):
        self.firstname = firstname
        self.lastname = lastname
        self.email= email
    def __eq__(self, other):
        # isinstance(other, Person)Remove
        if other is None or type(self) != type(other): return False
        # __dict__Compare attributes using methods
        return self.__dict__ == other.__dict__
    def __ne__(self, other):
        return not self.__eq__(other)

Now you can compare two objects in a generic way without having to write the attribute name. assertEquals also passes with a margin. It's assumed that the attribute has a eq method.

Recommended Posts

Implement __eq__ etc. generically in Python class
Implement Enigma in python
case class in python
Implement recommendations in Python
Implement XENO in python
Implement sum in Python
Implement Traceroute in Python 3
Class notation in Python
Implement naive bayes in Python 3.3
Implement ancient ciphers in python
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
Implement stacking learning in Python [Kaggle]
Implement R's power.prop.test function in python
Landmines hidden in Python class variables
Read PNG chunks in Python (class)
Implement the Singleton pattern in Python
Examine the object's class in python
Quickly implement REST API in Python
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
I tried to implement permutation in Python
Implement FIR filters in Python and C
Collectively implement statistical hypothesis testing in Python
I tried to implement PLSA in Python 2
Playing card class in Python (with comparison)
I tried to implement ADALINE in Python
I tried to implement PPO in Python
How to use __slots__ in Python class
Generate a class from a string in Python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
[Python] class, instance
Try to implement Oni Maitsuji Miserable in python
Geocoding in python
"Kanrika" python class
How to implement Discord Slash Command in Python
Meta-analysis in Python
Unittest in python
Unattended operation of Google Spreadsheets (etc.) in Python
About python, class
How to implement shared memory in Python (mmap.mmap)
[python] Difference between variables and self. Variables in class
I wrote a class in Python3 and Java
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Let's implement English voice dialogue in Python [offline]
Plink in Python
I tried to implement TOPIC MODEL in Python