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: Functional Programming HOWTO sub-optimal example for reduce
Type: Stage: resolved
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Anran Yang, docs@python, rhettinger
Priority: normal Keywords:

Created on 2017-09-25 12:11 by Anran Yang, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg302950 - (view) Author: Anran Yang (Anran Yang) Date: 2017-09-25 12:11
At the end of the Functional Programming HOWTO document (https://docs.python.org/3.7/howto/functional.html) the usage of reduce/lambda/for loops are compared and discussed. However, the example for reduce seems sub-optimal and thus the discussion is not that efficient. The example:

total = functools.reduce(lambda a, b: (0, a[1] + b[1]), items)[1]

could be changed to:

total = functools.reduce(lambda total, item: total + item[1], items, 0)

which is much more readable and is actually not much inferior to the loop one (though the sum approach is still more concise).
msg303015 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-09-26 06:41
I think the existing code conveys the author's message better than the proposed revision.  The example isn't about efficiency; rather, it is about comparing several alternative formulations with this one being the weakest of the lost.  For emphasizing that point and not getting lost in a made-up example, the a[1] and b[1] style is more effective.

That's for the suggestion but I don't think it is a win.
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75756
2017-09-26 06:41:01rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg303015

resolution: rejected
stage: resolved
2017-09-25 12:11:35Anran Yangcreate