Skip to content

Allow functools.reduces 'initial' to be a keyword argument #125916

Closed

Description

@sayandipdutta

Feature or enhancement

Proposal:

functools.reduce takes function (generally a callable) and iterable, with an optional initial parameter:

https://docs.python.org/3/library/functools.html#functools.reduce

However, initial cannot be passed as a keyword argument, which reduces ;) readability,

from functools import reduce
from operator import sub
>>> reduce(sub, [1, 1, 2, 3, 5, 8], 21)
1
>>> reduce(sub, [1, 1, 2, 3, 5, 8], initial=21)
TypeError: reduce() takes no keyword arguments

Allowing initial as keyword argument will be more clear, and initial could also be passed as a keyword argument while making partial functions out of reduce.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/remove-positional-only-restriction-on-initial-parameter-of-reduce/68897

Linked PRs

Activity

  1. skirpichev commented on Oct 24, 2024

    @skirpichev
    Member

    See also #121677

  2. skirpichev commented on Oct 24, 2024

    @skirpichev
    Member

    Also see #96640.

    @sobolevn, did you actually try to do conversion and benchmark it?

  3. added a commit that references this issue on Oct 26, 2024
  4. added a commit that references this issue on Nov 1, 2024
  5. added a commit that references this issue on Nov 12, 2024
  6. added 2 commits that reference this issue on Dec 8, 2024
  7. added 2 commits that reference this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions