Why does Python produce different sets with the same input?

Divide the following example:

>>> {1, True}
set([True])
>>
>>> {True, 1}
set([1])

Why are many represented differently, depending on the order of the elements?

+4
source share
2 answers

This is because 1, and Trueequal to each other:

>>> True == 1
True
>>> 1 == True
True

The set stores one element from each equality class.

+4
source

bool is a subclass of int class

>>> issubclass(bool, int)
True

>>> True+1
2
>>> True == 1
True
0
source

All Articles