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

后端 未结 7 1853
暗喜
暗喜 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:24

    The behavior of and and or exists for historical reasons.

    Before Python had a ternary operation / conditional expression, you used and and or if you wanted to use a value on a condition. Any such expression can be rewritten with the conditional expression syntax:

    true_val if condition else false_val
    

    Essentially, they are overloaded with two functions, and for compatibility reasons, they haven't been changed.

    That is not a reason to overload other operations. any seems like it should tell you whether or not a condition is true for any item, which is a boolean, so it should return a bool.

    0 讨论(0)
提交回复
热议问题