In the documentation of defaultdict
there is this example:
>>> def constant_factory(value):
... return itertools.repeat(value).next
as a "A faster and more flexible way to create constant functions".
How is this superior to
def constant_factory(value):
return lambda : value
?
Note that of course no one would define a function for that... just using (lambda: 42)
is clearer and shorter to type than calling constant_factory(42)
.
For creating a constant factory returning a mutable if that is wanted one could use (lambda x=[]:x)
(btw this is what would do constant_factory([])
, but it's something that often bites back ... see for example this question).
That being said, the iterators from itertools are often significantly faster than regular iteration from a standard Python for loop.
Itertools is a module in Python, it is used to iterate over data structures that can be stepped over using a for-loop. Such data structures are also known as iterables. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra.
itertools.permutations(iterable[, r])This tool returns successive length permutations of elements in an iterable. If is not specified or is None , then defaults to the length of the iterable, and all possible full length permutations are generated. Permutations are printed in a lexicographic sorted order.
The itertools. combinations() function takes two arguments—an iterable inputs and a positive integer n —and produces an iterator over tuples of all combinations of n elements in inputs .
Amazingly enough using itertools.repeat(value).next
is actually about 30% two-three times faster in both Python 2 and Python 3 (with the obvious variation of __next__
).
It's not much but also there's no reason to waste it.
PS: I'd say this shows that lambda
could be improved (I see no logical reason for having a bound method faster than a closure) but lambda
is not really loved in the Python community.
The reason for this is that itertools
primitives are implemented in C while a lambda executes Python bytecode. Just returning a captured value is really fast, but however it's still bytecode and requires a lot of setup/teardown like any Python function call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With