[Python-Dev] product()
Guido van Rossum
guido at python.org
Sat Oct 25 17:14:32 EDT 2003
> it might be "alltrue" and "anytrue" -- the short-circuiting ones,
> returning the first true or false item found respectively, as in:
>
> def alltrue(seq):
> for x in seq:
> if not x: return x
> else:
> return True
>
> def anytrue(seq):
> for x in seq:
> if x: return x
> else:
> return False
>
> these seem MUCH more generally useful than 'product' (but,
> I still opine, not quite enough to warrant being built-ins).
These are close to what ABC does with quantifiers. There, you can
write
IF EACH x IN sequence HAS x > 0: ...
ABC has the additional quirk that if there's an ELSE branch, you can
use x in it (as a "counter-example").
In Python, you could write this as
if alltrue(x > 0 for x in sequence): ...
but the current design doesn't expose x to the else branch.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list