[issue46379] itertools.product reference implementation creates temporaries

Markus Wallerberger report at bugs.python.org
Fri Jan 14 17:58:52 EST 2022


Markus Wallerberger <markus.wallerberger at tuwien.ac.at> added the comment:

> To a person well versed in recursion and in generator chains it makes sense but not so much for anyone else.

There I pretty much fundamentally disagree.  I find the version in the docs much more magical in the sense that it builds up "laterally", i.e., level-by-level, rather than element-by-element.

Also, I think from a functional programming perspective, which, let's face it, is what these iteration/generator tools are really modelling, a recursive version is much more natural.  It also generalizes nicely to other problems which people may be having -- so it has the added benefit of explaining the code and teaching people useful patterns.

Take the itertools.permutation as an example:  writing that as it was in the reference implementation the code is IMHO pretty opaque and hard to reason about.  Write it in a recursive style and both its working and correctness is immediately obvious.

>  Plus it is hard to step through by hand to see what it is doing.

This I agree with.

Anyway, thanks for taking the time to explain the rejection.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46379>
_______________________________________


More information about the Python-bugs-list mailing list