[Python-ideas] Break the dominance of boolean values in boolean context
Sven Marnach
sven at marnach.net
Fri Sep 16 15:30:23 CEST 2011
Amaury Forgeot d'Arc schrieb am Fr, 16. Sep 2011, um 08:16:58 +0200:
> Le 15 sept. 2011 19:55, "Alexander Belopolsky"
> <[1]alexander.belopolsky at gmail.com> a écrit :
> > 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
>
> It is the same for "a or b", even when a is true.
It's not the same -- the statement "if a or b:" will check the truth
values of "a" and "b" at most once in CPython, while an updated
version of "any()" would check the truth value of the found object
twice -- since "any()" is an ordinary function, it can't easily be
special-cased in the compiler.
This seems like a good reason to make "any()" behave differently than
"or" (apart from this behaviour being the current status quo anyway).
-- Sven
More information about the Python-ideas
mailing list