Method Analysis of python Implementation Comparison Class Two instance of Objects Are Equal

  • 2021-07-03 00:28:25
  • OfStack

This article illustrates how the python implementation compares the equality of two instance (objects) of a class. Share it for your reference, as follows:

For the same Class, you can create different instances (instance). How do you compare whether these two instance are equal? As we know, for a computer, to judge whether two objects are equal is to see whether the addresses in memory are the same. If the memory address is 1, it must be equal. This situation usually occurs when one object is a reference to another object.

However, in the actual development process, to compare whether two objects are equal or not, it is not judged by memory address, but by comparing some or all attribute values of these two objects.

Suppose there is one employee Class, and we create two objects respectively


class Staff(object):
 def __init__(self,id,name,sex):
  self.id=id
  self.name=name
  self.sex=sex

We think that if id is the same, it means that two objects are equal, and id means ID number for the moment. If ID number is the same, it must be the same person, which will happen in actual projects.

Create objects and view their respective memory addresses


staff1=Staff("123456"," Zhang 3"," Male ")
staff2=Staff("123456"," Li 4"," Female ")
print id(staff1),id(staff2)
#12327248 12327184

The result is obvious, in different memory addresses, if you judge at this time staff1==staff2 The result must be False.

How to meet our needs, as long as the same objects in id are considered equal objects, there are several methods as follows:

1. Overload the __eq__ method of Staff Class


class Staff(object):
 def __init__(self,id,name,sex):
  self.id=id
  self.name=name
  self.sex=sex
 def __eq__(self,other):
  return self.id==other.id
staff1=Staff("123456"," Zhang 3"," Male ")
staff2=Staff("123456"," Li 4"," Female ")
print id(staff1),id(staff2)
print staff1==staff2
#True

The result is returned as true, indicating that it is equal, but the memory address in the computer is definitely different. The __eq__ method is overloaded here. Of course, you can also add comparison conditions. In the example, only id is compared. In the same way, you can also realize operations such as adding two objects and overload the __add__ method.

2. Direct attribute value comparison


staff1=Staff("123456"," Zhang 3"," Male ")
staff2=Staff("123456"," Li 4"," Female ")
print id(staff1),id(staff2)
print staff1.__dict__
print staff2.__dict__
if staff1.__dict__['id']==staff2.__dict__['id']:
 print 'yes,equal'

You will find that this is also possible, and it is also possible to compare multiple attributes. The emphasis here is to use the __dict__ system built-in method of python Class.

More readers interested in Python can check the topics of this site: "Introduction and Advanced Tutorial of Python Object-Oriented Programming", "Python Data Structure and Algorithm Tutorial", "Summary of Python Function Use Skills", "Summary of Python String Operation Skills", "Summary of Python Coding Operation Skills" and "Introduction and Advanced Classic Tutorial of Python"

I hope this article is helpful to everyone's Python programming.


Related articles: