Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency between functools.reduce & itertools.accumulate #78840

Closed
lycantropos mannequin opened this issue Sep 13, 2018 · 11 comments
Closed

Inconsistency between functools.reduce & itertools.accumulate #78840

lycantropos mannequin opened this issue Sep 13, 2018 · 11 comments
Assignees
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@lycantropos
Copy link
Mannequin

lycantropos mannequin commented Sep 13, 2018

BPO 34659
Nosy @tim-one, @rhettinger, @kristjanvalur, @bitdancer, @serhiy-storchaka, @lisroach, @tirkarthi, @lycantropos
PRs
  • bpo-34659: Adds initial kwarg to itertools.accumulate() #9345
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/lisroach'
    closed_at = <Date 2018-09-25.01:23:04.357>
    created_at = <Date 2018-09-13.13:16:58.964>
    labels = ['3.8', 'type-feature', 'library']
    title = 'Inconsistency between functools.reduce & itertools.accumulate'
    updated_at = <Date 2018-09-25.01:23:04.356>
    user = 'https://github.com/lycantropos'

    bugs.python.org fields:

    activity = <Date 2018-09-25.01:23:04.356>
    actor = 'lisroach'
    assignee = 'lisroach'
    closed = True
    closed_date = <Date 2018-09-25.01:23:04.357>
    closer = 'lisroach'
    components = ['Library (Lib)']
    creation = <Date 2018-09-13.13:16:58.964>
    creator = 'lycantropos'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34659
    keywords = ['patch']
    message_count = 11.0
    messages = ['325243', '325245', '325303', '325305', '325513', '325611', '325616', '325626', '325643', '325848', '326195']
    nosy_count = 8.0
    nosy_names = ['tim.peters', 'rhettinger', 'kristjan.jonsson', 'r.david.murray', 'serhiy.storchaka', 'lisroach', 'xtreak', 'lycantropos']
    pr_nums = ['9345']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue34659'
    versions = ['Python 3.8']

    @lycantropos
    Copy link
    Mannequin Author

    lycantropos mannequin commented Sep 13, 2018

    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?

    @lycantropos lycantropos mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 13, 2018
    @bitdancer
    Copy link
    Member

    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.

    @bitdancer bitdancer added the 3.8 only security fixes label Sep 13, 2018
    @rhettinger rhettinger self-assigned this Sep 13, 2018
    @rhettinger
    Copy link
    Contributor

    I'm open to adding the feature as a keyword-only argument.

    Lisa, would you like to bring this to fruition?

    @rhettinger rhettinger assigned lisroach and unassigned rhettinger Sep 13, 2018
    @lisroach
    Copy link
    Contributor

    Happy to! I'll try to make a patch.

    @serhiy-storchaka
    Copy link
    Member

    This looks like a duplicate of bpo-25193.

    See also a long discussion on the Python-ideas mailing list 5 months ago: Start argument for itertools.accumulate().

    @rhettinger
    Copy link
    Contributor

    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?

    @tim-one
    Copy link
    Member

    tim-one commented Sep 18, 2018

    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 ;-)

    @serhiy-storchaka
    Copy link
    Member

    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 bpo-25718) 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 bpo-25193 and the discussion on Python-ideas), is it worth to add it?

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented Sep 18, 2018

    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 :)

    @rhettinger
    Copy link
    Contributor

    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.

    @lisroach
    Copy link
    Contributor

    New changeset 9718b59 by Lisa Roach in branch 'master':
    bpo-34659: Adds initial kwarg to itertools.accumulate() (GH-9345)
    9718b59

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants