any() and all() on empty list?

Ron Adam rrr at ronadam.com
Wed Mar 29 19:02:22 EST 2006


Paul Rubin wrote:
> Ron Adam <rrr at ronadam.com> writes:
>> Just thinking about things.  I really just want what is best for
>> Python in the long term and am not trying to be difficult.
> 
> I'm sorry, maybe it's the math geek in me, but I just see all those
> suggestions about "not not S" as being contorted.  It's obvious to me
> that all([]) should be True, that while(any(S)) should not terminate
> if S is empty, etc.

The 'not not S' is just a conversion to bool. Is the following less 
contorted to you?

 >>> bool([])
False

> Someone asked for a cite; I listed one before:
> 
> http://en.wikipedia.org/wiki/For_all
> 
> See the "Negation" section.


'Is all True' isn't the same as 'Has all True'.  As I said, I'm not 
questioning the mathematical meaning of the set relation 'is all True', 
but wondering weather or not an alternate relation 'has all True' would 
be better for use as a flow control test.


Do you have some examples uses since it's obvious to you?

We could all probably come up with examples that support either side. 
What I'm looking for are the obvious and common use examples.  How would 
they behave differently depending on weather 'is all true' or 'has all 
true' is used?  Which would be faster and simpler to use in most cases.

I just have a feeling we will see a lot of "S and all(S)" expressions 
being used.  Maybe that's not so bad,  but I would prefer to not have to 
do that if it turns out to the standard idiom for all testing within a loop.


The actual code used would be more efficient than my examples, they will 
have shortcutting behavior, and written in C.  Those examples where 
meant to show the principle.

And the question still stands:

    "Does it do the right thing in most situations it will be used in?"


That will of course depend on weather it's being used as a mathematics 
test, or for flow control test.  Which is why I suggested the possibly 
of having both. I believe the flow control semantics will be the more 
common use, but I may be mistaken thinking "S and all(S)" will be needed 
in most cases.

<shrug>This doesn't seem to be an issue for anyone else, so I'll wait 
and see how it turns out.

Cheers,
    Ron




More information about the Python-list mailing list