[Python-Dev] Negative times behaviour in itertools.repeat for Python maintenance releases (2.7, 3.3 and maybe 3.4)

Vajrasky Kok sky.kok at speaklikeaking.com
Tue Jan 28 03:26:17 CET 2014


On Mon, Jan 27, 2014 at 9:13 PM, Larry Hastings <larry at hastings.org> wrote:
>
>
> While it's a bug, it's a very minor bug.  As Python 3.4 release manager, my
> position is: Python 3.4 is in beta, so let's not change semantics for
> purity's sakes now.  I'm -0.5 on adding times=None right now, and until we
> do we can't deprecate the old behavior.
>
>

I bow to your decision, Larry. So I believe the doc fix is required
then. I propose these for doc fix:

1. Keeps the status quo
=================

>>> repeat.__doc__
'repeat(object [,times]) -> create an iterator which returns the
object\nfor the specified number of times.  If not specified, returns
the object\nendlessly.'

We don't explain the meaning of negative `times`. Well, people
shouldn't repeat with negative times because statement such as, "Kids,
repeat the push-up
negative two times more.", does not make sense.

2. Explains the negative times, ignores the keyword
=====================================

>>> repeat.__doc__
'repeat(object [,times]) -> create an iterator which returns the
object\nfor the specified number of times.  If not specified, returns
the object\nendlessly.
Negative times means zero repetitions.'

The signature repeat(object [,times]) suggest this function does not
accept keyword as some core developers have stated. So if the user
uses keyword with this function,
well, it's too bad for them.

3. Explains the negative times, warns about keyword
======================================

>>> repeat.__doc__
'repeat(object [,times]) -> create an iterator which returns the
object\nfor the specified number of times.  If not specified, returns
the object\nendlessly.
Negative times means zero repetitions.  This function accepts keyword
argument but the behaviour is buggy and should be avoided.'

4. Explains everything
================

>>> repeat.__doc__
'repeat(object [,times]) -> create an iterator which returns the
object\nfor the specified number of times.  If not specified, returns
the object\nendlessly.
Negative times means zero repetitions via positional-only arguments.
-1 value for times via keyword means endless repetitions and is same
as omitting
times argument and other negative number for times means endless
repetitions as well but with different implementation.'

If you are wondering about the last statement:
>>> from itertools import repeat
>>> list(repeat('a', times=-4))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t
>>> a = repeat('a', times=-4)
>>> next(a)
'a'
>>> next(a)
'a'
>>> a = repeat('a', times=-1)
>>> next(a)
'a'
>>> next(a)
'a'
>>> list(repeat('a', times=-1))
... freezes your computer ...

Which one is better? Once we settle this, I can think about the doc
fix for Doc/library/itertools.rst.

Vajrasky


More information about the Python-Dev mailing list