Overriding compare in SequenceMatcher

Tim Peters tim.one at comcast.net
Mon Jan 26 17:35:36 CET 2004

[Peter Galfi]
> I am trying to make a compare between two lists of numbers using the
> SequenceMatcher, however I would need somehow to override the method
> which is used by SequenceMatcher to determine if two elements of the
> list are equal or not.
> In my specific case a number of the list might be equivalent with
> another number of the list yet their values would be different.
> Any idea as to how what and how I can override or any workaround for
> this problem?

Elements of sequences are used internally as dict keys, so it's not enough
just to override comparison -- you also need to define a hash function
consistent with your idea of equality (objects that compare equal must
deliver the same hash codes).

So you need to wrap your numbers in a class, and define the class's __eq__
and __hash__ methods appropriately.  There's no need to define any
comparison method other than __eq__.

More information about the Python-list mailing list