Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 to implement oneself like this, but I'm still left wondering why.

def any(l):     for x in l:         if x:             return x     return x 

edit:

To add to the answers below, here's an actual quote from that same mailing list of ye mighty emperor on the issue:

Whether to always return True and False or the first faling / passing element? I played with that too before blogging, and realized that the end case (if the sequence is empty or if all elements fail the test) can never be made to work satisfactory: picking None feels weird if the argument is an iterable of bools, and picking False feels weird if the argument is an iterable of non-bool objects.

Guido van Rossum (home page: http://www.python.org/~guido/)

like image 748
doda Avatar asked Apr 16 '12 19:04

doda


People also ask

What is return bool in Python?

It returns True if the parameter or value passed is True. It returns False if the parameter or value passed is False.

Is it true that Python function always returns a value?

A Python function will always have a return value. There is no notion of procedure or routine in Python. So, if you don't explicitly use a return value in a return statement, or if you totally omit the return statement, then Python will implicitly return a default value for you.

How do you return true or false in Python?

x > y, x >= y : Returns True if Python object x is “greater than, greater than or equal to” y . Otherwise, returns False .

Can a function return a bool?

Boolean functions must return something boolean. Non-boolean functions must not return something boolean. Comparisons, logical AND/OR, and true/false macros are bool.


2 Answers

This very issue came up up on the Python developer's mailing list in 2005, when Guido Van Rossum proposed adding any and all to Python 2.5.

Bill Janssen requested that they be implemented as

def any(S):     for x in S:         if x:             return x     return S[-1]  def all(S):     for x in S:         if not x:             return x     return S[-1] 

Raymond Hettinger, who implemented any and all, responded specifically addressing why any and all don't act like and and or:

Over time, I've gotten feedback about these and other itertools recipes. No one has objected to the True/False return values in those recipes or in Guido's version.

Guido's version matches the normal expectation of any/all being a predicate. Also, it avoids the kind of errors/confusion that people currently experience with Python's unique implementation of "and" and "or".

Returning the last element is not evil; it's just weird, unexpected, and non-obvious. Resist the urge to get tricky with this one.

The mailing list largely concurred, leaving the implementation as you see it today.

like image 200
Steven Rumbalski Avatar answered Oct 18 '22 13:10

Steven Rumbalski


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.

like image 23
Weeble Avatar answered Oct 18 '22 11:10

Weeble