AgentBlueNote
AgentBlueNote

Reputation: 129

Why explicit test for an empty set does not work

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

Upvotes: 12

Views: 4865

Answers (3)

Yu Hao
Yu Hao

Reputation: 122483

The key point here is, False is not the only value that is considered falsy. An empty set is also interpreted as false in the condition.

However, the explicit equality test test == False is not true because an empty set, as definition, is of type set, which is never equal to a boolean value False.


See Boolean Operations:

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. User-defined objects can customize their truth value by providing a __bool__() method.

Upvotes: 3

Kasravnd
Kasravnd

Reputation: 107347

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.

Upvotes: 7

poke
poke

Reputation: 388163

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__.

Upvotes: 15

Related Questions