Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 870
AgentBlueNote Avatar asked Oct 31 '15 14:10

AgentBlueNote


2 Answers

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

like image 147
poke Avatar answered Oct 29 '22 00:10

poke


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.

like image 37
Mazdak Avatar answered Oct 29 '22 02:10

Mazdak