[issue19145] Inconsistent behaviour in itertools.repeat when using negative times

Raymond Hettinger report at bugs.python.org
Sun Jun 22 09:15:34 CEST 2014


Raymond Hettinger added the comment:

I'm inclined to apply Vajrasky Kok's third version (with minor cleanups).

The rule will be pretty much what Guido stated but without adding a special case for times=None (that could be an added enhancement at a later time if the need arose):  "If I had complete freedom in redefining the spec I would treat positional and keyword the same, interpret absent or None to mean "forever" and explicit negative integers to mean the same as zero, and make repr show a positional integer >= 0 if the repeat isn't None."

The if-absent-run-forever rule matches what the decade old positional-only API does and what the newer keyword form does as well.  It also matches what the documented rough equivalent code does.

The negative-times-means-zero rule matches the current positional-only api, it matches list.__mul__ and str.__mul__, and it matches the documented equivalent code.   However, it does change the meaning of the keyword argument when the value is negative (the part that conflicts with the positional API, was never intended, nor was promised in the docs).

Larry's false dilemmas aside, I think that takes care of the core issue that a negative value for a keyword times-argument does not have the same behavior as it would for a positional times-argument.

The use of "None" for an optional argument in the "equivalent" code is red herring.  As Serhiy says, the "sample Python implementation is only a demonstration, it shouldn't be exact equivalent."  If Larry still perceives this to be "wildly out of sync", it isn't hard to put in the usual "times=sentinel" setup in the same code, but that only adds a little precision at the expense of clarity (i.e. readers are more likely to be confused by the sentinel trick than by the conventional way of noting optional arguments with None).

----------

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


More information about the Python-bugs-list mailing list