If empty set()
is False
, shouldn't the if test == False
clause in the following code evaluate to True
?
test = set()
# empty sets are false
if test == False:
print("set is empty")
else:
print("set NOT empty")
if not test:
print("set is empty")
ouput:
set NOT empty
set is empty
In simple terms, the equals operator ==
will perform an equality comparison between those two objects: A set and a boolean value will never be equal, so the result of the comparison is false. On the other hand, just checking if obj
(or if not obj
) will check the trueness of the object, something that can be evaluated for every object. In a way, this actually does a type conversion using if bool(obj)
. And for empty sets, this is false.
In the data model, both of these operations invoke different special method names. Comparing two objects using the equality operator will invoke __eq__
while calling bool()
on an object will invoke __bool__
.
The operator ==
will check the values of 2 object and in this case an empty set()
and a False value have not a same value.
And since python evaluates any empty sequence as False and none empty sequences as True, if you want to check the validation of the test
object you can simple use if
:
if test:
#do stuff
Truth Value Testing
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:
List item
None
False
zero of any numeric type, for example, 0, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines a
__bool__()
or__len__()
method, when that method returns the integer zero or bool value False.All other values are considered true — so objects of many types are always true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With