“Boolean” operations in Python (ie: the and/or operators)

后端 未结 5 1757
一生所求
一生所求 2020-11-30 12:34

This method searches for the first group of word characters (ie: [a-zA-Z0-9_]), returning the first matched group or None in case of failure.

5条回答
  •  时光说笑
    2020-11-30 13:21

    Basically a and b returns the operand that has the same truth value as the whole expression.

    It might sound a bit confusing but just do it in your head: If a is False, then b does not matter anymore (because False and anything will always be False), so it can return a right away.

    But when a is True then only b matters, so it returns b right away without even looking.

    This is a very common and very basic optimization many languages do.

提交回复
热议问题