any() and all() on empty list?
Ron Adam
rrr at ronadam.com
Wed Mar 29 09:31:19 CEST 2006
Paul McGuire wrote:
> "Paul Rubin" <http://phr.cx@NOSPAM.invalid> wrote in message
> news:7x3bh1x0ym.fsf at ruckus.brouhaha.com...
> To my mind, the *meaning* of all() is that every element in the list asserts
> True. But this is with an initial assumption that all() is False, unless I
> test every value and find them to be True. Since I assume False to begin
> with, I get no values in the list to contradict the assumption, and so
> all([]) returns False.
Looking at in a different way... If we consider also having a function
none() (for comparison), would it be consistent with all()?
def none(S):
for x in S:
if x: return False
return True
any([]) -> False
none([]) -> True (same as 'not any(S)')
all([]) -> True ? False
I think I agree that all() should have an initial presumption of being
False.
Looking at it in yet another way... (yes, not as efficient)
def all(S):
S_ = [x for x in S if x]
return S_ == S
def any(S):
S_ = [x for x in S if x]
return S_ != []
def none(S):
S_ = [x for x in S if x]
return S_ == []
In this view and empty set can be True for all(). Is it posible
'all([])' is undefined? Here, none() and all() return contradicting
values. So maybe the correct version may be...
def all(S):
if S == []: return False
for x in S:
if x return True
return False
I think a few valid actual use case examples could clear it up. What
makes the most sense?
Cheers,
Ron
More information about the Python-list
mailing list