Reputation: 21
Python 3.7: Why does
{'A'} not in {'A'}
evaluate to true?
Shouldn't the correct answer be 'false'? {'A'}.is
Following code does work for me:
not {'A'}.issubset({'A'})
But I'd like to understand why the first one won't.
Upvotes: 2
Views: 95
Reputation: 44888
({'A'} not in {'A'}) is True
({'A'} in {'B', frozenset({'A'})}) is True
{'A'}.issubset({'A'}) is True
('A' not in {'A'}) is False
{'A'}
doesn't literally contain the set {'A'}
. It contains 'A'
. {frozenset({'A'})}
, however, does contain an inner set frozenset({'A'})
and a string 'B'
. It's not possible to write {{'A'}}
because that would require that the inner set be hashable, but sets are not hashable, while frozenset
s are.
Also {'A'}
is a subset of itself, which is different from it literally containing a copy of itself.
Upvotes: 1
Reputation: 428
So, 'A'
is a string, while {'A'}
is a set, containing a string 'A'
. Your right operand {'A'}
does not contain your left operand {'A'}
(it only contains 'A'
), so the whole expression is true. But if you write 'A' not in {'A'}
, the result would be False
.
Upvotes: 0