[docs] [issue18313] In itertools recipes repeatfunc() defines a non-keyword argument as keyword

py.user report at bugs.python.org
Thu Aug 8 19:19:55 CEST 2013


py.user added the comment:

> This would require you to provide at least two elements
I see
how about "def repeatfunc(func, *args, times=None):" ?


>>> from itertools import starmap, repeat
>>> 
>>> def repeatfunc(func, *args, times=None):
...     """Repeat calls to func with specified arguments.
... 
...     Example:  repeatfunc(random.random)
...     """
...     if times is None:
...         return starmap(func, repeat(args))
...     return starmap(func, repeat(args, times))
... 
>>> def f(*args):
...     print(args)
... 
>>> r = repeatfunc(f, 1, 2)
>>> next(r)
(1, 2)
>>> next(r)
(1, 2)
>>> next(r)
(1, 2)
>>> r = repeatfunc(f, 1, 2, times=1)
>>> next(r)
(1, 2)
>>> next(r)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>>

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18313>
_______________________________________


More information about the docs mailing list