What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like
Example:
doc1 = ErrorDocument(path='/folder',title='Page')
doc2 = ErrorDocument(path='/folder',title='Page')
if doc1 == doc2: # this should be True
#do something
EDIT:
To further clarify the question. I'd like to compare by attribute values, and to make a more generic solution than
def __eq__(self, other):
return self.path == other.path and self.title == other.title
Should the __eq__()
method look something like this?
def __eq__(self, other):
# Is the other instance of the same object
# Loop through __dict__ and compare values to attributes of other