I cannot add the integer number 1 to an existing set. In an interactive shell, this is what I am doing:
>>> st = {'a', True, 'Vanilla'}
>>> st
{'a', True, 'Vanilla'}
>>> st.add(1)
>>> st
{'a', True, 'Vanilla'} # Here's the problem; there's no 1, but anything else works
>>> st.add(2)
>>> st
{'a', True, 'Vanilla', 2}
This question was posted two months ago, but I believe it was misunderstood.
I am using Python 3.2.3.
I believe your problem is that
1andTrueare the same value, so 1 is “already in the set”.In mathematical operations
Trueis itself treated as1:Though True is a bool and 1 is an int:
Because
1 in streturns True, I think you shouldn’t have any problems with it. It is a very strange result though. If you’re interested in further reading, @Lattyware points to PEP 285 which explains this issue in depth.