Python - the logical order of evaluation in the expression "if"

In Python, we can do this:

if True or blah: print("it ok") # will be executed if blah or True: # will raise a NameError print("it not ok") class Blah: pass blah = Blah() if blah or blah.notexist: print("it ok") # also will be executed 
  • Can someone point me to the documentation for this feature?
  • Is this an implementation detail or a language feature?
  • Is this a good coding style for using this feature?
+7
source share
4 answers

Short circuit of or and and , see documentation Logical operations :

The expression x and y first evaluates x ; if x 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.

Note that for and , y is evaluated only if x evaluates to True. Conversely, for or , y is evaluated only if x is evaluated as False.

For the first True or blah expression, this means that blah never evaluated, since the first part is already True .

In addition, your custom blah class is considered True:

In the context of Boolean operations, and also 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 strings, tuples, lists, dictionaries, sets, and freezes). All other values ​​are interpreted as true. (See __nonzero__() special way to change this parameter.)

Since your class does not implement the __nonzero__() method (or the __len__ method), it is considered True with respect to Boolean expressions.

In the expression blah or blah.notexist , blah is thus true, and blah.notexist never evaluated.

This function is used quite regularly and effectively by experienced developers, most often specifying default values:

 some_setting = user_supplied_value or 'default literal' object_test = is_it_defined and is_it_defined.some_attribute 

Use caution when binding them and use a conditional expression instead, where applicable.

+21
source

This is called short-circuited and is a sign of language:

http://docs.python.org/2/tutorial/datastructures.html#more-on-conditions

The Boolean operators and and or are the so-called short-circuit operators: their arguments are evaluated from left to right, and the evaluation stops as soon as the result is determined. For example, if A and C are true, but B is false, A and B and C do not evaluate the expression C. When used as a common value, and not as a Boolean, the return value of the short circuit operator is the last evaluated argument.

+5
source

This is how logical operators work, in particular or in python: short circuit assessment.

To better explain this, consider the following:

 if True or False: print('True') # never reaches the evaluation of False, because it sees True first. if False or True: print('True') # print True, but it reaches the evaluation of True after False has been evaluated. 

For more information, see the following:

+2
source

Using the or operator evaluates values ​​from left to right. After one value is evaluated as True , the entire statement is evaluated as True (therefore, no more values ​​are calculated).

+1
source

All Articles