Python - logical evaluation order in “if” statement

前端 未结 4 1463
耶瑟儿~
耶瑟儿~ 2020-12-03 12:02

In Python we can do this:

if True or blah:
    print(\"it\'s ok\") # will be executed

if blah or True: # will raise a NameError
    print(\"it\'s not ok\")
         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-03 13:01

    With the or operator, values are evaluated from left to right. After one value evaluates to True, the entire statement evaluates to True (so no more values are evaluated).

    • Official documentation
    • It's a feature of the language
    • There is nothing wrong with using its functionality

提交回复
热议问题