What is a good example of the __eq__ method for a collection class?

I am working on a collection class for which I want to create an __eq__ method. It turned out to be more subtle than I thought it would be, and I noticed a few subtleties of how built-in collection classes work.

What really helps me the most is a good example. Are there any pure Python implementations of the __eq__ method either in the standard library or in any third-party libraries?

+6
python equality collections api
source share
2 answers

Take a look at "collections.py". The latest version (from version control) implements OrderedDict with __eq__. There is also __eq__ in sets.py

+1
source share

Parts are heavy. Parts should be simple delegation.

 def __eq__( self, other ): if len(self) != len(other): # Can we continue? If so, what rule applies? Pad shorter? Truncate longer? else: return all( self[i] == other[i] for i in range(len(self)) ) 
+7
source share

All Articles