What's the value of short-circuit of Python?

前端 未结 4 1216
太阳男子
太阳男子 2021-01-24 13:46

I\'m learning the book named Data Structures & Algorithms in Python.

On Page 12 that introduce the Logical Operators, it writes:

4条回答
  •  半阙折子戏
    2021-01-24 14:33

    >>> if 1 or 0:
    ...     print "I am learning"
    ... 
    I am learning
    >>> if 1 and 0:
    ...     print "I am learning"
    ... 
    >>> if 0:
    ...     print "I am learning"
    ... 
    >>> if 1:
    ...     print "I am learning"
    ... 
    I am learning
    >>> if None and 1:
    ...     print "be crazy"
    ... 
    >>> if None or 1:
    ...     print "be crazy"
    be crazy
    

    much like C implementation.

    And the last line is read correct had the first condition was unsuccessful in returning the error the second condition checks for the error

提交回复
热议问题