Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Comparing empty string to False is False, why?

If not '' evaluates to True, why does '' == False evaluates to False?

For example, the "voids" of the other types (e.g. 0, 0.0) will return True when compared to False:

>>> 0 == False
True
>>> 0.0 == False
True

Thanks

like image 714
Tony Power Avatar asked Dec 04 '22 00:12

Tony Power


2 Answers

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.

The operator not yields True if its argument is false, False otherwise.

https://docs.python.org/3/reference/expressions.html#comparisons

But:

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects do not need to have the same type.

...

Because all types are (direct or indirect) subtypes of object, they inherit the default comparison behavior from object. Types can customize their comparison behavior by implementing rich comparison methods like __lt__() ...

https://docs.python.org/3/reference/expressions.html#boolean-operations

So, the technical implementation answer is that it behaves the way it does because not and == use different comparisons. not uses __bool__, the "truth value" of an object, while == uses __eq__, the direct comparison of one object to another. So it's possible to ask an object whether it considers itself to be truthy or falsey, and separately from that ask it whether it considers itself to be equal to another object or not. The default implementations for that are arranged in a way that two objects can both consider themselves falsey yet not consider themselves equal to one another.

like image 139
deceze Avatar answered Dec 11 '22 08:12

deceze


It doesn't make sense for '' and [] to actually equal False, because they are clearly different values: a string and a list. If they both equalled False they would have to be equal to each other*. They are just "falsey", which means they come out as false when they are converted to a boolean.

(* in any sensibly constructed language)

not is an operation that returns a boolean. Which boolean it returns depends on whether the operand is falsey or not. So not x is not equivalent to x==False; it is equivalent to bool(x)==False.

like image 20
khelwood Avatar answered Dec 11 '22 09:12

khelwood