Adding 1 to a set containing True does not work

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 .

+6
source share
1 answer

For historical (hysterical?) Reasons, the Python bool type is a subclass of int , and True is 1 and False is 0.

They have a hash in the same place:

 >>> True == 1 True >>> hash(True) == hash(1) True >>> False == 0 True >>> hash(False) == hash(0) True 

Since both True and 1 are considered equal, and they hashed into the same slot, both set and dict treat them as one and the same.

You will see the same with the dictionary:

 >>> True in {1: 'foo'} True >>> 1 in {True: 'foo'} True 

This behavior extends to other numbers; floating point values ​​equal to integer values ​​will show the same behavior:

 >>> {1.0, 1, 2.0, 2} {1, 2} 

but at least the reasons why this happens are a little more .. obvious.

+7
source

Source: https://habr.com/ru/post/951817/


All Articles