This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve reduce example in doanddont.rst
Type: Stage:
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: cito, eric.araujo, ezio.melotti, georg.brandl, rhettinger
Priority: low Keywords:

Created on 2009-11-27 10:47 by cito, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg95762 - (view) Author: Christoph Zwerschke (cito) * Date: 2009-11-27 10:47
In the section "Using the batteries" of the "Idioms and Anti-Idioms in
Python" document
(http://docs.python.org/dev/howto/doanddont.html#using-the-batteries),
the reduce statement is used for summing up numbers as an example. I
think this is rather an anti-example, because Python already has a sum
function built-in, i.e. reduce(operator.add, nums)/len(nums) can be
written much simpler as sum(nums)/len(nums).
msg96286 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-12-12 08:22
The example alone can't be fixed, because half of the section talks
about how useful reduce() is.
The whole document should IMHO be rewritten, possibly using some of the
examples in
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
I already asked to David and he said that the license (CC-BY-SA) allows
to do that.
If you agree to rewrite it and no one else volunteer, I can try to do
it, but I don't know when I'll have time.
msg96287 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-12-12 08:40
FWIW, I think the example is fine as-is.  It could be changed to a
product example:  reduce(operator.mul, range(1, n+1)) or somesuch. 
Also, the text could be modified to mention sum().  But it is also fine
if the example is unchanged, it does show how reduce() works.

Recommend closing or just having a very minor edit.
msg96308 - (view) Author: Christoph Zwerschke (cito) * Date: 2009-12-12 21:41
My point was that the passage starts with "there are also many useful
built-in functions people seem not to be aware of for some reasons" and
then it looks like the author himself was not aware of sum() for some
reason because he gives calculating a sum with reduce() as a "classical
example".

It's very hard to come up with good examples for reduce() and I think in
newer Python versions it has been demoted from builtin to functools, so
it's not a good example for a useful built-in fuction anyway.
msg115840 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-08 01:57
I agree with Raymond that the example should be kept. We could add a comment explaining the difference with sum: reduce + operator.add works with all types. (sum would be marked up so that a link gets generated.)

Is there a subsection about functional programming yet? reduce, filter and map are not always easy to use, especially now that they are lazy and thus can’t be used as a poor man’s for loop (i.e. for side effects, no to build a list). Such a section would be short and link to the functional programming howto.

In the long term, integrating David Goodger’s document into the main docs so that it gets more exposure and more updates seems very useful to me.
msg120093 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-10-31 22:02
Fixed in r86070, r86071, and r86072.
History
Date User Action Args
2022-04-11 14:56:55adminsetgithub: 51651
2010-10-31 22:03:30rhettingersetstatus: open -> closed
resolution: fixed
2010-10-31 22:02:36rhettingersetmessages: + msg120093
2010-09-08 01:57:19eric.araujosettitle: Imrpove reduce example in doanddont.rst -> Improve reduce example in doanddont.rst
2010-09-08 01:57:10eric.araujosettitle: reduce() is an anti-example in "Idioms and Anti-Idioms" -> Imrpove reduce example in doanddont.rst
messages: + msg115840
versions: - Python 2.6
2010-09-08 01:25:25ezio.melottisetnosy: + eric.araujo
2009-12-13 01:20:39rhettingersetassignee: rhettinger
2009-12-12 21:41:49citosetmessages: + msg96308
2009-12-12 08:40:57rhettingersetassignee: rhettinger -> (no value)
messages: + msg96287
2009-12-12 08:22:53ezio.melottisetpriority: low
versions: - Python 3.0
nosy: + ezio.melotti

messages: + msg96286
2009-11-27 20:08:49rhettingersetassignee: georg.brandl -> rhettinger

nosy: + rhettinger
2009-11-27 10:47:21citocreate