[Python-ideas] Break the dominance of boolean values in boolean context
Paul Moore
p.f.moore at gmail.com
Thu Sep 15 15:56:30 CEST 2011
On 15 September 2011 14:40, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
>> FWIW, I just came to python-ideas to propose the same change to any()
>> for the same use case (look-up in a pseudo dict). I needed this
>> several times now, and I would consider the change to any()
>> worthwhile. (For consistency, all() would also have to be changed,
>> though I didn't have a use case yet.)
>
> As someone suggested earlier in this thread, this can be achieved with
> a very simple expression (not even a function!):
>
> next(filter(None, S))
>
> will return the first item x from S with bool(x) evaluating to True.
> Further simplifying this is not worth slowing down all current uses of
> any().
And if the special-case behaviour of filter(None, ...) upsets you, a
generator expression is just as good:
next(x for x in S if x)
That's simple and readable enough (to my mind). In fact, it's more
obvious to me than any(S) returning a non-boolean result would be...
Paul.
More information about the Python-ideas
mailing list