Strange Conversion in Python Logical Expressions

I noticed the strange behavior of Python 2.7 logical expressions:

  >>> 0 and False
 0
 >>> False and 0
 False
 >>> 1 and False
 False
 >>> False and 1
 False

and with True instead of False

  >>> 0 and True
 0
 >>> True and 0
 0
 >>> 1 and True
 True
 >>> True and 1
 1

Are there any rules when Python converts a boolean operator to an integer? Why does it sometimes show 0 false and 1 True instances?

Also, why is he returning this?

  >>> "test" or "test"
 'test'
+6
source share
3 answers

Nothing is transformed; Python Boolean logic operators instead of short circuits.

See the documentation for boolean statements :

The expression x and y first evaluates x ; if x is false, its value is returned; otherwise, y is evaluated and the return value is returned.

The expression x or y first evaluates x ; if x true, its value is returned; otherwise, y is evaluated and the return value is returned.

In addition, numbers equal to 0 are considered false, as are empty lines and containers. Quoting from the same document:

In the context of Boolean operations, as well as when expressions are used by flow control operators, the following values ​​are interpreted as false: False , None , numeric zero of all types, and empty lines and containers (including lines, tuples, lists, dictionaries, sets, and freezes).

Combining these two actions means that for 0 and False 0 is considered false and is returned before evaluating the False expression. For the expression True and 0 , True is evaluated and considered a true value, therefore 0 returned. As for if and while and other Boolean operators, this result 0 also considered false.

You can use this to provide a default value, for example:

 foo = bar or 'default' 

To really convert a non-Boolean value to a boolean, use bool() type ; it uses the same rules as logical expressions to determine the logical value of input:

 >>> bool(0) False >>> bool(0.0) False >>> bool([]) False >>> bool(True and 0) False >>> bool(1) True 

To complete the image, values ​​that are not considered false in a boolean context are considered true, including any custom classes. You can change this by implementing the .__nonzero__() special method in your class. If no such method is defined, .__len__() also be consulted. Using any of these methods, you can indicate that your type is numeric and should be considered True if it is not equal to zero, or it is a container and should be considered True if it is not empty (has a length greater than 0).

+7
source

Python always returns one of the operand objects

and returns the first "false" object or the final "true" object.

or returns the first "true" object or the final "false" object.

Note that all 0 , 0.0 , False , "" are considered "false."

 >>> "a" and "b" and "c" 'c' >>> "a" or "b" or "c" 'a' 
+3
source

This is because 0 is the most fake value.

 >>> bool(0) False >>> bool(1) True 

0 and True is 0 , because 0 is Falsy, the AND condition ceases to be executed as soon as the first false value is found and returns this value. If all values ​​were True, the rightmost value is returned.

OR will continue to check the values ​​until the first True is found, otherwise it will return the last value. (righmtmost)

From docs :

Any object can be checked for true, for use in an if or while condition, or as an operand of the following Boolean operations. The following values ​​are considered false:

  • None

  • False

  • zero of any number type, for example 0, 0L, 0.0, 0j .

  • any empty sequence, for example '', (), [] .

  • any empty mapping, for example {} .

  • instances of custom classes if the class defines the __nonzero__() or __len__() method when this method returns the integer 0 or the value bool False.

All other values ​​are considered true, so objects of many types are always true.

Operations and built-in functions that have a logical result always return 0 or False for false and 1 or True for true, unless otherwise specified. (An important exception: logical operations or and and always return one of their operands.)

+2
source

Source: https://habr.com/ru/post/944763/


All Articles