Why aren't “and” and “or” operators in Python?

前端 未结 6 594
陌清茗
陌清茗 2021-02-07 00:32

I wasn\'t aware of this, but apparently the and and or keywords aren\'t operators. They don\'t appear in the list of python operators. Just out of sh

6条回答
  •  梦毁少年i
    2021-02-07 00:55

    Python does not currently provide any 'xxx' special methods corresponding to the 'and', 'or' and 'not' boolean operators. In the case of 'and' and 'or', the most likely reason is that these operators have short-circuiting semantics, i.e. the second operand is not evaluated if the result can be determined from the first operand. The usual technique of providing special methods for these operators therefore would not work.

    Source: PEP 335

    PEP 335 talks about adding the ability to have overloadable operators, and discusses this issue a bit.

提交回复
热议问题