Python – What is the trick behind Python’s any() function?

What is the trick behind Python’s any() function?… here is a solution to the problem.

What is the trick behind Python’s any() function?

One of the pycon2011 talks shares this any() function trick; The interpretation is cycling in C.

Can someone explain? What is the trick behind it? Are there other use cases?

>>> import itertools, hashlib, time
>>> _md5 = hashlib.md5()
>>> def run():
...   for i in itertools.repeat('foo', 10000000):
...     _md5.update(i)
... 
>>> a = time.time(); run(); time.time() -a
3.9815599918365479
>>> _md5 = hashlib.md5()
>>> def run():
...   any(itertools.imap(_md5.update, itertools.repeat('foo', 10000000)))
... 
>>> a = time.time(); run(); time.time() -a
2.1475138664245605
>>> 

Solution

itertools.imap creates a lazy list of functions to evaluate (md5) and their arguments (‘foo’ string). MD5 calls are not evaluated at this point, but are prepared along with their parameters (I think they are called thunks). When you then pass this iterator to any function, it iterates through all the elements that evaluated them. This happens faster than the explicit Python evaluation of the first program because any is implemented in C and everything happens in the C library code without going back to the interpreter after each iterator element.

Related Problems and Solutions