Q:

hashlib - why does a digest match but the bare object doesn't?

I am wondering what the practical reason there is for the observed behavior in python 2.7:
import hashlib
hashlib.md5('foo') == hashlib.md5('foo')

Returns false.  But...
import hashlib
hashlib.md5('foo').hexdigest() == hashlib.md5('foo').hexdigest()

Returns true.  I understand that the hexdigest/digest return the final string representations, but since the same data has been entered into the two hash objects directly, shouldn't they evaluate as equal to one another?  Wouldn't the md5 HASH object be aware of the internal identity when the magic __eq__ methods are called?  For what reason would the objects themselves evaluate inequality?  Really just curious.

A:

There's no equality comparison for hashlib.md5 objects so, as they are different objects, the result is false.
The .hexdigest method generates a string (or byte string if you're on Python 3) and strings can be compared.
BTW:
x = hashlib.md5('foo')
x == x # is True because it's the same object

