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

Ethan Furman ethan at stoneleaf.us
Thu Sep 15 20:40:40 CEST 2011


Alexander Belopolsky wrote:
> On Thu, Sep 15, 2011 at 1:45 PM, Guido van Rossum <guido at python.org> wrote:
> ..
>> Considering the use cases, and Python's pervasive use of
>> any-object-as-Boolean, I think it would have been fine if they had
>> been defined as follows:
>>
>> def any(xs):
>>  x = False
>>  for x in xs:
>>    if x: break
>>  return x
> 
> Did you consider that in a typical "if any(S):" construct,
> x.__bool__() will be called twice on the found object?  It is not
> unheard of to have expensive __bool__().  For example in a vector
> library a vector may be considered "false" if all its components are
> zero.

If the calculation is expensive, shouldn't the vector cache the result 
for subsequent access?

~Ethan~



More information about the Python-ideas mailing list