]> granicus.if.org Git - python/commitdiff
Add a note on optimizing the itertools recipes for production.
authorRaymond Hettinger <python@rcn.com>
Sun, 28 Mar 2010 18:08:15 +0000 (18:08 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 28 Mar 2010 18:08:15 +0000 (18:08 +0000)
Doc/library/itertools.rst

index a978c1afd2aa365b207eeb1f3f4193a2ec96deff..4f82a27d11dda3f1ced70b3cf4bda41fc7c88e62 100644 (file)
@@ -784,3 +784,9 @@ which incur interpreter overhead.
        except exception:
            pass
 
+Note, many of the above recipes can be optimized by replacing global lookups
+with local variables defined as default values.  For example, the
+*dotproduct* recipe can be written as::
+
+   def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul):
+       return sum(imap(mul, vec1, vec2))