Consider the hypothetical function repeatcall
, which takes no-args as the called func
and a positive integer n
and returns a list whose members are obtained by executing func()
n
times. It supports an endless stream of silly khinks, such as:
>>> repeatcall(lambda: id(dict()), 5) [45789920, 45788064, 45807216, 45634816, 45798640] >>> urandom = lambda: struct.unpack('Q', open('/dev/urandom').read(8))[0] >>> repeatcall(urandom, 3) [3199039843823449742, 14990726001693341311L, 11583468019313082272L] >>> class Counter(itertools.count): __call__ = itertools.count.next >>> repeatcall(Counter(100, -2), 4) [100, 98, 96, 94]
I can swear I saw a function like repeatcall
somewhere in the standard Python 2.x libraries, but I can't find it. If I hadn’t dreamed about it, where can I find it in the standard library ?
PS: I know that it is trivial to roll one, but I do not like to reinvent the wheels, especially those that are already in the standard library. I do not ask how to collapse.
Edit: made it even more explicit that I am not asking how to encode repeatcall
.
source share