Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Python itertools `constant_factory` example superior to `lambda : x`?

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).

like image 292
6502 Avatar asked Dec 17 '14 18:12

6502


People also ask

Is Itertools faster than for loops?

That being said, the iterators from itertools are often significantly faster than regular iteration from a standard Python for loop.

How does Itertools work in Python?

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.

How does Itertools permutations work?

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.

How does Itertools combinations work?

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 .


1 Answers

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.

like image 151
6502 Avatar answered Sep 22 '22 00:09

6502