classification
Title: Weird `slice.stop or sys.maxint`
Type: behavior Stage:
Components: Documentation Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: bbrazil, cool-RR, docs@python, ezio.melotti, mark.dickinson, rhettinger, serhiy.storchaka, ysj.ray
Priority: low Keywords: easy, patch

Created on 2011-04-22 17:37 by cool-RR, last changed 2013-07-11 07:24 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
issue11908-islice-docs.patch bbrazil, 2012-07-07 09:46 review
Messages (7)
msg134276 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-04-22 17:37
In the documentation for `itertools.islice` I see this line:

    it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))

Is it really okay to do `s.stop or sys.maxint`? I'm assuming this was targeting `None`, but what if `s.stop == 0`? And `s.step` could (pathologically) be `0` too, no?
msg134372 - (view) Author: ysj.ray (ysj.ray) Date: 2011-04-25 07:09
`step` argument for xrange() could not be 0.

But `s.stop or sys.maxint` is really a problem, in the case of `s.stop == 0`.

So the given `Equivalent to` python code in the doc is not precisely equivalent to the c implementation. The doc needs a fix.
msg134374 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-25 07:51
I've got from here.  Thanks.
msg164796 - (view) Author: Brian Brazil (bbrazil) * Date: 2012-07-07 09:46
The attached patch fixes this
msg176756 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-12-01 20:49
LGTM. However note, that for 2.7 the patch should be modified (maxsize -> maxint, range -> xrange).
msg176800 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-12-02 17:18
ysj:  The "equivalent" means "roughly equivalent" not "precisely equivalent".  The purpose of the code in the docs is to help communicate what islice() is all about.  Practicality beats purity in this regard.  I know of no one who has ever been mislead by the islice() docs.

I don't really like the proposed patch -- it improves a bit on technical accuracy but forgoes readability and still falls short on what stop=None does and on which arguments are required.  

That said, I don't really like the current version of the "roughly equivalent code either".  It should probably be rewritten completely (no longer using slice() and it should show the actual looping logic.  For clarity, it may be helpful to split it into two code-equivalents, one for the finite case (where stop is specified) and one for the infinite case (where stop is None).  

As it stands, the examples are beating the code equivalent when it comes to communicating what islice() does.  I don't want to lengthen the code fragment unless it becomes *both* more communicative and more accurate.
msg192848 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-07-11 07:24
Closing for the reasons lists above.
History
Date User Action Args
2013-07-11 07:24:27rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg192848
2013-07-07 21:29:50terry.reedysetversions: - Python 3.2
2012-12-27 20:15:49serhiy.storchakasetstage: commit review ->
2012-12-02 17:18:40rhettingersetmessages: + msg176800
2012-12-02 16:41:11rhettingersetpriority: normal -> low
2012-12-02 10:20:31mark.dickinsonsetnosy: + mark.dickinson
2012-12-01 20:49:22serhiy.storchakasetversions: + Python 3.4
nosy: + serhiy.storchaka

messages: + msg176756

stage: needs patch -> commit review
2012-07-07 09:46:55bbrazilsetfiles: + issue11908-islice-docs.patch

nosy: + bbrazil
messages: + msg164796

keywords: + patch
2011-11-19 13:38:38ezio.melottisetkeywords: + easy
nosy: + ezio.melotti
stage: needs patch

versions: - Python 3.1
2011-04-25 18:10:51terry.reedysetversions: - Python 2.6, Python 2.5, Python 3.4
2011-04-25 07:51:04rhettingersetmessages: + msg134374
2011-04-25 07:09:08ysj.raysetnosy: + ysj.ray
messages: + msg134372
2011-04-22 18:01:51rhettingersetassignee: docs@python -> rhettinger

nosy: + rhettinger
2011-04-22 17:37:25cool-RRcreate