How does a Python set([]) check if two objects are equal? What methods does an object need to define to customise this?

Yes, you need a __hash__()-method AND the comparing-operator which you already provided. class Item(object): def __init__(self, foo, bar): self.foo = foo self.bar = bar def __repr__(self): return “Item(%s, %s)” % (self.foo, self.bar) def __eq__(self, other): if isinstance(other, Item): return ((self.foo == other.foo) and (self.bar == other.bar)) else: return False def __ne__(self, other): return (not self.__eq__(other)) … Read more

How do I deal with “signed/unsigned mismatch” warnings (C4018)?

It’s all in your things.size() type. It isn’t int, but size_t (it exists in C++, not in C) which equals to some “usual” unsigned type, i.e. unsigned int for x86_32. Operator “less” (<) cannot be applied to two operands of different sign. There’s just no such opcodes, and standard doesn’t specify, whether compiler can make … Read more

java.lang.IllegalArgumentException: Comparison method violates its general contract [duplicate]

Your compare() method is not transitive. If A == B and B == C, then A must be equal to C. Now consider this case: For A, B, and C, suppose the containsKey() method return these results: childMap.containsKey(A.getID()) returns true childMap.containsKey(B.getID()) returns false childMap.containsKey(C.getID()) returns true Also, consider orders for A.getId() != B.getId(). So, A … Read more