I am interested in the truth value of Python sets like {'a', 'b'}, or the empty set set() (which is not the same as the empty dictionary {}). In particular, I would like to know whether bool(my_set) is False if and only if the set my_set is empty.
Ignoring primitive (such as numerals) as well as user-defined types, https://docs.python.org/3/library/stdtypes.html#truth says:
The following values are considered false:
- [...]
- any empty sequence, for example,
'',(),[].- any empty mapping, for example,
{}.- [...]
All other values are considered true
According to https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range, a set is not a sequence (it is unordered, its elements do not have indices, etc.):
There are three basic sequence types: lists, tuples, and range objects.
And, according to https://docs.python.org/3/library/stdtypes.html#mapping-types-dict,
There is currently only one standard mapping type, the dictionary.
So, as far as I understand, the set type is not a type that can ever be False. However, when I try, bool(set()) evaluates to False.
Questions:
- Is this a documentation problem, or am I getting something wrong?
- Is the empty set the only set whose truth value is
False?
setbuilt-in type came relatively late to the game (version 2.2 or 2.3). They likely never updated the docs here to addor an empty setdicts as well? It's been around pretty much since the beginning, but was omitted.dictwas addressed here: "any empty mapping, for example {}"