[Python-ideas] Break the dominance of boolean values in boolean context

Steven D'Aprano steve at pearwood.info
Thu Sep 15 01:04:12 CEST 2011


Mike Graham wrote:
> On Wed, Sep 14, 2011 at 9:12 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> Perhaps 'need' is putting it too strongly, but if any()/all() were changed
>> to return the first true-ish/last false-ish value, then the extra parameter
>> would be convenient in specifying the value desired if it wasn't True/False.
>>  Sort of like {}.get('nothere', '5').
>>
>> ~Ethan~
> 
> It's worth noting that this implementation of any would make it
> suspiciously similar to next.


I don't see that. next() returns the next item from an iterator 
regardless of its boolean state. The actual and proposed implementations 
of any() skip over false items. The proposal might be implemented 
something like this:


def any(iterable):
     item = False
     for item in iterable:
         if item:
             return item  # currently return True
     return item


The practical difference should be clear if you compare calling:

next(iter([0]*1000 + [1]))

versus:

any([0]*1000 + [1])




-- 
Steven




More information about the Python-ideas mailing list