Python set interpetation of 1 and True

A set is a collection of hashables. Even though the statement 1 is True is False, the statement 1 == True is True. Because of that, they have the same hash value and cannot exist separately in a set, and you cannot keep them both in a set

EDIT To make it explicit, as jme pointed out, it is because BOTH things are true – they are equal (per __eq__) AND they have the same hash value (per __hash__).

In a perfect world, equal objects would also have the same hash value, and thankfully this is true for built-in types.

Leave a Comment