On 31 March 2017 at 15:12, Nick Coghlan <ncogh...@gmail.com> wrote: > So *if* we were to add anything to the language here, it would be to > add `itertools.repeat_call` as a new iteration primitive, since it > isn't entirely straightforward to construct that operation out of the > existing primitives, with itertools.starmap coming closest: > > def repeat_call(callable, n): > yield from starmap(callable, repeat((), n)) > > But the explicit for loop being clearest: > > def repeat_call(callable, n): > for __ in range(n): > yield callable()
It occurred to me to check whether or not `more_itertools` already had a suitable operation here, and it does: https://more-itertools.readthedocs.io/en/latest/api.html#more_itertools.repeatfunc This is the `repeatfunc` recipe from the itertools documentation: https://docs.python.org/3/library/itertools.html#itertools-recipes Cheers, Nick. -- Nick Coghlan | ncogh...@gmail.com | Brisbane, Australia _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/