[Python-ideas] Break the dominance of boolean values in boolean context
python at mrabarnett.plus.com
Wed Sep 14 19:19:45 CEST 2011
On 14/09/2011 17:17, Alexander Belopolsky wrote:
> On Tue, Sep 13, 2011 at 8:59 PM, Ned Batchelder<ned at nedbatchelder.com> wrote:
>> .. Why shouldn't any() also return Sx?
> What is the use case for returning the first "trueish" value? In most
> cases, any() is used in boolean context, say
> if any(S):
> In this case, returning Sx would result in Sx.__bool__() being called
> twice. It is also rare that any() is called on the raw sequence of
> objects that are of interest. More often it is used to express
> predicates such as any(x< 0 for x in S).
I had a use-case recently.
I was looking for an entry in a pseudo-dict, where the key didn't have
to be an exact match, so I called the .get method on the pseudo-dict
until it returned a true-ish value (the value would never be false-ish).
I just wrote a short function to do what I wanted.
More information about the Python-ideas