Why does python `any` return a bool instead of the value?

后端 未结 7 1861
暗喜
暗喜 2020-12-08 18:28

and and or return the last element they evaluated, but why doesn\'t Python\'s built-in function any?

I mean it\'s pretty easy

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 19:23

    and and or can be sensibly defined in a way that they always return one of their operands. However, any and all cannot sensibly be defined always to return a value from their input sequence: specifically they cannot do so when the list is empty. Both any and all currently have a well defined result in this situation: any returns False and all returns True. You would be forced to sometimes return a boolean value and sometimes return an item from the sequence, which makes for an unpleasant and surprising interface. Much better to be simple and consistent.

提交回复
热议问题