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

Guido van Rossum guido at python.org
Thu Sep 15 19:58:48 CEST 2011


On Thu, Sep 15, 2011 at 10:55 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> 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.

I hadn't just now, but it is possible that this was part of the
original reasoning for always returning True or False.

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list