Associativity of “in” in Python?

后端 未结 4 770
既然无缘
既然无缘 2021-01-30 15:15

I\'m making a Python parser, and this is really confusing me:

>>>  1 in  []  in \'a\'
False

>>> (1 in  []) in \'a\'
TypeError: \'in &         


        
4条回答
  •  半阙折子戏
    2021-01-30 16:14

    The short answer, since the long one is already given several times here and in excellent ways, is that the boolean expression is short-circuited, this is has stopped evaluation when a change of true in false or vice versa cannot happen by further evaluation.

    (see http://en.wikipedia.org/wiki/Short-circuit_evaluation)

    It might be a little short (no pun intended) as an answer, but as mentioned, all other explanation is allready done quite well here, but I thought the term deserved to be mentioned.

提交回复
热议问题