Issue 19202: Additions to function docs: reduce and itertools. (original) (raw)

Created on 2013-10-09 10:09 by Esa.Peuha, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
doc.diff Esa.Peuha,2013-10-09 10:09 review
reduce_equiv.diff georg.brandl,2013-10-12 16:55 review
Messages (11)
msg199281 - (view) Author: Esa Peuha (Esa.Peuha) Date: 2013-10-09 10:09
Here are some additions to documentation of a few functions: all, any: alternative definitions using functools.reduce enumerate: alternative definition using zip and itertools.count sum: equivalent definition using functools.reduce and operator.add functools.reduce: equivalent definitions, use operator.add in example itertools.accumulate: point to functools.reduce itertools.filterfalse: point to filter
msg199282 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-10-09 10:29
Most of these changes should not be applied: the "alternate" equivalents in terms of reduce() will not help understanding, Equivalents for reduce() may be useful, but I would limit them to one per case, possibly even just one function that covers both cases. I'm deferring to Raymond for the changes to the itertools docs.
msg199313 - (view) Author: Esa Peuha (Esa.Peuha) Date: 2013-10-09 16:58
How would you give a single definition of reduce() that helps people to understand both 2-arg and 3-arg variants? The way it is implemented in C is impossible to duplicate in pure Python; the best you could do is a hack that works unless someone *tries* to break it, but that would just be confusing.
msg199315 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-10-09 17:13
What about def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: value = next(it) else: value = initializer for element in it: value = function(value, element) return value Remember, an equivalent doesn't have to be 100% compatible, it is a way for the user to quickly get an idea what the function does.
msg199527 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-10-12 00:14
I think the reduce equivalent given by Georg would be excellent. I think the enumerate equivalent already given is fine because I think equivalents should use basic syntax (for loops), not something that might be more obscure.
msg199541 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-10-12 07:20
Almost all of these make the docs worse and should not be applied. The purpose of the "equivalent" code is simply to make the documentation clearer, not to show all the ways it could have been done. I do think Georg's reduce() equivalent should be added because it is clearer than the current prose description.
msg199589 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-10-12 16:20
What do you think of the two references added to the itertools docs?
msg199597 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-10-12 16:44
The reference from accumulate() to reduce() may be useful. I'm opposed to most of the other equivalents and cross-references. They bloat the docs without adding value. Terry is right in saying that the equivalent for enumerate() is better as a basic loop than as a composition of functional tools. I think the OP has missed what the purpose of the code equivalents was trying to do.
msg199599 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-10-12 16:51
I agree. Will prepare a patch to the reduce() doc.
msg199639 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-12 23:05
New changeset 3b6401c27e39 by Raymond Hettinger in branch '3.3': Issue #19202: Add cross-reference and a rough code equivalent http://hg.python.org/cpython/rev/3b6401c27e39
msg199640 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-10-12 23:05
Thanks Georg.
History
Date User Action Args
2022-04-11 14:57:51 admin set github: 63401
2013-10-12 23:05:51 rhettinger set status: open -> closedresolution: fixedmessages: +
2013-10-12 23:05:09 python-dev set nosy: + python-devmessages: +
2013-10-12 16:55:14 georg.brandl set files: + reduce_equiv.diff
2013-10-12 16:51:52 georg.brandl set messages: +
2013-10-12 16:44:14 rhettinger set messages: +
2013-10-12 16:20:50 georg.brandl set messages: +
2013-10-12 07:20:14 rhettinger set messages: +
2013-10-12 07:15:50 rhettinger set assignee: docs@python -> rhettinger
2013-10-12 00:14:02 terry.reedy set nosy: + terry.reedytitle: Additions to docs -> Additions to function docs: reduce and itertools.messages: + versions: + Python 3.3
2013-10-09 17:13:17 georg.brandl set messages: +
2013-10-09 16:58:09 Esa.Peuha set messages: +
2013-10-09 10:29:57 georg.brandl set nosy: + georg.brandl, rhettingermessages: +
2013-10-09 10:09:16 Esa.Peuha create