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: Inconsistency between functools.reduce & itertools.accumulate
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: lisroach Nosy List: kristjan.jonsson, lisroach, lycantropos, r.david.murray, rhettinger, serhiy.storchaka, tim.peters, xtreak
Priority: normal Keywords: patch

Created on 2018-09-13 13:16 by lycantropos, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9345 merged lisroach, 2018-09-16 21:18
Messages (11)
msg325243 - (view) Author: Azat Ibrakov (lycantropos) Date: 2018-09-13 13:16
Why there is an optional `initial` parameter for `functools.reduce` function, but there is no such for `itertools.accumulate`, when they both are doing kind of similar things except that `itertools.accumulate` yields intermediate results and `functools.reduce` only the final one?
msg325245 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-09-13 14:15
Presumably because conceptually an 'initial value' would be like adding an additional element on to the front of the iterable being passed as an argument, and itertools is all about operating on iterables.  I'm not saying such an argument could not be added, but the API is cleaner without it.
msg325303 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-09-13 23:07
I'm open to adding the feature as a keyword-only argument.

Lisa, would you like to bring this to fruition?
msg325305 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2018-09-13 23:13
Happy to! I'll try to make a patch.
msg325513 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-17 05:54
This looks like a duplicate of issue25193.

See also a long discussion on the Python-ideas mailing list 5 months ago: [Start argument for itertools.accumulate()](https://mail.python.org/pipermail/python-ideas/2018-April/049649.html).
msg325611 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-09-18 04:08
Tim, do we care about whether "initial=None" versus "initial=_sentinel"?

The former makes for a nice looking pure python equivalent and works nicely with the argument clinic to generate a signature.  However, it precludes using None as the actual start argument (though I don't see why somewone would want to do that), and it isn't parallel to reduce() (not sure I care about that at all). 

Kristjan, do you want to contribute to the update of the pickle/copy code for this API extension?
msg325616 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2018-09-18 05:44
Ya, I care:  `None` was always intended to be an explicit way to say "nothing here", and using unique non-None sentinels instead for that purpose is needlessly convoluted.  `initial=None` is perfect.  But then I'm old & in the way ;-)
msg325626 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-18 10:34
The first issue, with ignoring initial=None, is complex because Argument Clinic and the inspect module don't support optional parameters without default value. It could be possible to use optional groups, but currently Argument Clinic supports optional groups only when all parameters are positional-only. For now, the only way is getting rid from Argument Clinic.

As for pickling/copying, this task is technically difficult, it but I don't see principal problems. The code is already complex (see issue25718) and will be more complex. Since the reduce protocol doesn't support keyword arguments, we will needs to use either partial() for passing the keyword argument, or chain() for imitating it by creating an equivalent object. I can write this code.

Taking to the account the complexity of the implementation and arguments against this feature (see issue25193 and the discussion on Python-ideas), is it worth to add it?
msg325643 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2018-09-18 13:45
I think I'll pass Raymond, its been so long since I've contributed, in the mean time there is github and argument clinic and whatnot so I'm out of training.  I´m lurking around these parts and maybe shall return one day :)
msg325848 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-09-20 07:53
> As for pickling/copying, this task is technically difficult, 
> it but I don't see principal problems. 

I've added the pickle/copy support to the PR.

> is it worth to add it?

Tim was persuasive, so I've agreed to add the feature.  It may be little used but may help once in a while.
msg326195 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2018-09-24 00:35
New changeset 9718b59ee5f2416cdb8116ea5837b062faf0d9f8 by Lisa Roach in branch 'master':
bpo-34659: Adds initial kwarg to itertools.accumulate() (GH-9345)
https://github.com/python/cpython/commit/9718b59ee5f2416cdb8116ea5837b062faf0d9f8
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78840
2018-09-25 01:23:04lisroachsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-09-24 00:35:04lisroachsetmessages: + msg326195
2018-09-20 07:53:43rhettingersetmessages: + msg325848
2018-09-18 13:45:45kristjan.jonssonsetmessages: + msg325643
2018-09-18 10:34:42serhiy.storchakasetmessages: + msg325626
2018-09-18 05:44:05tim.peterssetnosy: + tim.peters
messages: + msg325616
2018-09-18 04:08:45rhettingersetmessages: + msg325611
2018-09-17 06:43:18rhettingersetnosy: + kristjan.jonsson
2018-09-17 06:22:01xtreaksetnosy: + xtreak
2018-09-17 05:54:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg325513
2018-09-16 21:18:45lisroachsetkeywords: + patch
stage: patch review
pull_requests: + pull_request8769
2018-09-13 23:13:19lisroachsetmessages: + msg325305
2018-09-13 23:07:39rhettingersetassignee: rhettinger -> lisroach

messages: + msg325303
nosy: + lisroach
2018-09-13 16:22:20rhettingersetassignee: rhettinger

nosy: + rhettinger
2018-09-13 14:15:17r.david.murraysetnosy: + r.david.murray

messages: + msg325245
versions: + Python 3.8, - Python 3.5
2018-09-13 13:16:58lycantroposcreate