<div class="gmail_quote">On Tue, Sep 25, 2012 at 1:55 PM, Richard D. Moores <span dir="ltr"><<a href="mailto:rdmoores@gmail.com" target="_blank">rdmoores@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

I was just perusing the Built-in Functions of Python 3.2 (<<a href="http://docs.python.org/py3k/library/functions.html" target="_blank">http://docs.python.org/py3k/library/functions.html</a>>) and was wondering where would one ever use any() or all(). <br>



<br>But so what? Could I get some better examples?<br></blockquote><div><br></div><div>I frequently use any() or all() in combination with a generator expression to check for a certain condition over a list of elements. Let's say, for example, I want to make sure all files I used are closed at the end of a function (or perhaps a unit test?)</div>

<div><br></div><div>assert all(f.closed for f in open_files)</div><div><br></div><div>probably not the *most* useful example of that, but I'm sure you understand the principle. It's useful when you need to check if all() or any() of an iterator pass a certain condition.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>And why<br><font face="courier new, monospace">>>> all([])<br>

True<br>>>> any([])<br>False</font><br></blockquote><div><br></div><div>People often wonder over this one. any([]) should logically be False, because it is like asking the question "is there any member in this iterable that is True?" Obviously, there is not, since the iterator is empty. all([]) is True by the principle of vacuous truth: </div>

<div><br></div><div><a href="http://en.wikipedia.org/wiki/Vacuous_truth">http://en.wikipedia.org/wiki/Vacuous_truth</a></div><div><br></div><div>The principle, though somewhat intuitive, has a strong mathematical basis.</div>

<div><br></div><div>HTH,</div><div>Hugo</div></div>