I recently started to learn Python and came across something strange when playing with sets. The following code example does not produce the expected results.
a_set = {True,2,3,4} a_set.add(1)
I expected a_set to have the values {True, 1, 2, 3, 4} , but instead this code produced {True, 2, 3, 4} .
Trying variations on this also produced the same results:
a_set = {1,2,3,4} a_set.add(True)
Expected {True, 1, 2, 3, 4} Actual {1, 2, 3, 4}
Trying to do this with False and 0 got the same results:
a_set = {False,2,3,4} a_set.add(0)
Expected {False, 0, 2, 3, 4} Actual {False, 2, 3, 4}
a_set = {0,2,3,4} a_set.add(False)
Expected {False, 0, 2, 3, 4} Actual {0, 2, 3, 4}
I understand that the bool type inherits from int and that True == 1 and False == 0 , but still a little surprised by the results above.
Does anyone know if this is by design? Is it also possible to have a set that contains both True , False , 0 , and 1 ?
I performed quite a lot of searches, but could not find the answer to my questions.
Thank you in advance
UPDATE
In response to the comments below, I agree that the following question partially answers my question.
Is False == 0 and True == 1 in Python an implementation detail, or is it guaranteed by the language?
But I feel that it is not responding to the request that I have regarding the behavior of the sets, and whether it has a set containing both True and 1 . Even if bool inherits from int , they are different types, so I found that the set cannot distinguish between True and 1 , to confuse a little. So, really, this is a question about the behavior of sets in Python, not only about True == 1 .